blob: f81e7d9bfd2a6f504e7bbac30f383c2a4a0b1f1b [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 Kandoif18ec9f2015-09-28 13:35:59 -070044#define ALWAYS_PLUGGED_CAPACITY 100
Ruchi Kandoi5c09ec12016-02-25 16:19:30 -080045#define MILLION 1.0e6
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070046#define DEFAULT_VBUS_VOLTAGE 5000000
Todd Poynor752faf22013-06-12 13:25:59 -070047
48namespace android {
49
50struct sysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -070051 const char* s;
Todd Poynor752faf22013-06-12 13:25:59 -070052 int val;
53};
54
55static int mapSysfsString(const char* str,
56 struct sysfsStringEnumMap map[]) {
57 for (int i = 0; map[i].s; i++)
58 if (!strcmp(str, map[i].s))
59 return map[i].val;
60
61 return -1;
62}
63
Yabin Cuidb04a492016-02-16 17:19:23 -080064static void initBatteryProperties(BatteryProperties* props) {
65 props->chargerAcOnline = false;
66 props->chargerUsbOnline = false;
67 props->chargerWirelessOnline = false;
68 props->maxChargingCurrent = 0;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070069 props->maxChargingVoltage = 0;
Yabin Cuidb04a492016-02-16 17:19:23 -080070 props->batteryStatus = BATTERY_STATUS_UNKNOWN;
71 props->batteryHealth = BATTERY_HEALTH_UNKNOWN;
72 props->batteryPresent = false;
73 props->batteryLevel = 0;
74 props->batteryVoltage = 0;
75 props->batteryTemperature = 0;
76 props->batteryCurrent = 0;
77 props->batteryCycleCount = 0;
78 props->batteryFullCharge = 0;
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -070079 props->batteryChargeCounter = 0;
Yabin Cuidb04a492016-02-16 17:19:23 -080080 props->batteryTechnology.clear();
81}
82
83BatteryMonitor::BatteryMonitor() : mHealthdConfig(nullptr), mBatteryDevicePresent(false),
84 mAlwaysPluggedDevice(false), mBatteryFixedCapacity(0), mBatteryFixedTemperature(0) {
85 initBatteryProperties(&props);
86}
87
Todd Poynor752faf22013-06-12 13:25:59 -070088int BatteryMonitor::getBatteryStatus(const char* status) {
89 int ret;
90 struct sysfsStringEnumMap batteryStatusMap[] = {
91 { "Unknown", BATTERY_STATUS_UNKNOWN },
92 { "Charging", BATTERY_STATUS_CHARGING },
93 { "Discharging", BATTERY_STATUS_DISCHARGING },
94 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
95 { "Full", BATTERY_STATUS_FULL },
96 { NULL, 0 },
97 };
98
99 ret = mapSysfsString(status, batteryStatusMap);
100 if (ret < 0) {
101 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
102 ret = BATTERY_STATUS_UNKNOWN;
103 }
104
105 return ret;
106}
107
108int BatteryMonitor::getBatteryHealth(const char* status) {
109 int ret;
110 struct sysfsStringEnumMap batteryHealthMap[] = {
111 { "Unknown", BATTERY_HEALTH_UNKNOWN },
112 { "Good", BATTERY_HEALTH_GOOD },
113 { "Overheat", BATTERY_HEALTH_OVERHEAT },
114 { "Dead", BATTERY_HEALTH_DEAD },
115 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
116 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
117 { "Cold", BATTERY_HEALTH_COLD },
118 { NULL, 0 },
119 };
120
121 ret = mapSysfsString(status, batteryHealthMap);
122 if (ret < 0) {
123 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
124 ret = BATTERY_HEALTH_UNKNOWN;
125 }
126
127 return ret;
128}
129
Michael Scott3217c5c2016-06-05 11:20:13 -0700130int BatteryMonitor::readFromFile(const String8& path, std::string* buf) {
131 if (android::base::ReadFileToString(String8::std_string(path), buf)) {
132 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700133 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700134 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700135}
136
137BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700138 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700139 BatteryMonitor::PowerSupplyType ret;
140 struct sysfsStringEnumMap supplyTypeMap[] = {
141 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
142 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
143 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
144 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
145 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
146 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Johan Redestig32828612016-02-03 13:45:54 +0100147 { "USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Todd Poynor752faf22013-06-12 13:25:59 -0700148 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
149 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
Benson Leung8a4eef62015-10-07 16:12:04 -0700150 { "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC },
151 { "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC },
152 { "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB },
Todd Poynor752faf22013-06-12 13:25:59 -0700153 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
154 { NULL, 0 },
155 };
156
Michael Scott3217c5c2016-06-05 11:20:13 -0700157 if (readFromFile(path, &buf) <= 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700158 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
159
Michael Scott3217c5c2016-06-05 11:20:13 -0700160 ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf.c_str(), supplyTypeMap);
Johan Redestig32828612016-02-03 13:45:54 +0100161 if (ret < 0) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700162 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700163 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100164 }
Todd Poynor752faf22013-06-12 13:25:59 -0700165
166 return ret;
167}
168
169bool BatteryMonitor::getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700170 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700171 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700172
173 if (readFromFile(path, &buf) > 0)
174 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700175 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700176
177 return value;
178}
179
180int BatteryMonitor::getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700181 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700182 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700183
184 if (readFromFile(path, &buf) > 0)
185 value = std::stoi(buf.c_str(), NULL, 0);
186
Todd Poynor752faf22013-06-12 13:25:59 -0700187 return value;
188}
189
190bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700191 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700192
Yabin Cuidb04a492016-02-16 17:19:23 -0800193 initBatteryProperties(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700194
Todd Poynorf5d30122013-08-12 17:03:35 -0700195 if (!mHealthdConfig->batteryPresentPath.isEmpty())
196 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700197 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700198 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700199
Todd Poynor3db03a52014-05-21 16:28:13 -0700200 props.batteryLevel = mBatteryFixedCapacity ?
201 mBatteryFixedCapacity :
202 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700203 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700204
Ruchi Kandoicc338802015-08-24 13:01:16 -0700205 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
206 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
207
208 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
209 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
210
211 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
212 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
213
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700214 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
215 props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
216
Todd Poynor3db03a52014-05-21 16:28:13 -0700217 props.batteryTemperature = mBatteryFixedTemperature ?
218 mBatteryFixedTemperature :
219 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700220
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700221 // For devices which do not have battery and are always plugged
222 // into power souce.
223 if (mAlwaysPluggedDevice) {
224 props.chargerAcOnline = true;
225 props.batteryPresent = true;
226 props.batteryStatus = BATTERY_STATUS_CHARGING;
227 props.batteryHealth = BATTERY_HEALTH_GOOD;
228 }
229
Michael Scott3217c5c2016-06-05 11:20:13 -0700230 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700231
Michael Scott3217c5c2016-06-05 11:20:13 -0700232 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
233 props.batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700234
Michael Scott3217c5c2016-06-05 11:20:13 -0700235 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
236 props.batteryHealth = getBatteryHealth(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700237
Michael Scott3217c5c2016-06-05 11:20:13 -0700238 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
239 props.batteryTechnology = String8(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700240
241 unsigned int i;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700242 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700243
244 for (i = 0; i < mChargerNames.size(); i++) {
245 String8 path;
246 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
247 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700248 if (getIntField(path)) {
249 path.clear();
250 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
251 mChargerNames[i].string());
252 switch(readPowerSupplyType(path)) {
253 case ANDROID_POWER_SUPPLY_TYPE_AC:
254 props.chargerAcOnline = true;
255 break;
256 case ANDROID_POWER_SUPPLY_TYPE_USB:
257 props.chargerUsbOnline = true;
258 break;
259 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
260 props.chargerWirelessOnline = true;
261 break;
262 default:
263 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
264 mChargerNames[i].string());
265 }
266 path.clear();
267 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
268 mChargerNames[i].string());
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700269 int ChargingCurrent =
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700270 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
271
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700272 path.clear();
273 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
274 mChargerNames[i].string());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700275
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700276 int ChargingVoltage =
277 (access(path.string(), R_OK) == 0) ? getIntField(path) :
278 DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700279
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700280 double power = ((double)ChargingCurrent / MILLION) *
281 ((double)ChargingVoltage / MILLION);
282 if (MaxPower < power) {
283 props.maxChargingCurrent = ChargingCurrent;
284 props.maxChargingVoltage = ChargingVoltage;
285 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700286 }
287 }
288 }
289
Todd Poynor10b235e2013-08-07 15:25:14 -0700290 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700291
Todd Poynor10b235e2013-08-07 15:25:14 -0700292 if (logthis) {
293 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700294 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700295 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700296 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700297 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
298 props.batteryLevel, props.batteryVoltage,
299 props.batteryTemperature < 0 ? "-" : "",
300 abs(props.batteryTemperature / 10),
301 abs(props.batteryTemperature % 10), props.batteryHealth,
302 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700303
Ruchi Kandoicc338802015-08-24 13:01:16 -0700304 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700305 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700306 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
307 " c=%d", props.batteryCurrent);
308 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700309
Ruchi Kandoicc338802015-08-24 13:01:16 -0700310 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
311 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
312 " fc=%d", props.batteryFullCharge);
313 }
314
315 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
316 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
317 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700318 }
319 } else {
Ruchi Kandoie9320b72016-02-22 12:46:57 -0800320 len = snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700321 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700322 }
323
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700324 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
325 props.chargerAcOnline ? "a" : "",
326 props.chargerUsbOnline ? "u" : "",
327 props.chargerWirelessOnline ? "w" : "");
328
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700329 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700330 }
331
Todd Poynorc7464c92013-09-10 12:40:00 -0700332 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700333 return props.chargerAcOnline | props.chargerUsbOnline |
334 props.chargerWirelessOnline;
335}
336
Yabin Cuiaedf6032016-02-19 18:03:23 -0800337int BatteryMonitor::getChargeStatus() {
338 int result = BATTERY_STATUS_UNKNOWN;
339 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700340 std::string buf;
341 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
342 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800343 }
344 return result;
345}
346
Todd Poynorc133b712013-08-14 17:39:13 -0700347status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
348 status_t ret = BAD_VALUE;
349
Todd Poynor8f132af2014-05-08 17:15:45 -0700350 val->valueInt64 = LONG_MIN;
351
Todd Poynorc133b712013-08-14 17:39:13 -0700352 switch(id) {
353 case BATTERY_PROP_CHARGE_COUNTER:
354 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700355 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700356 getIntField(mHealthdConfig->batteryChargeCounterPath);
357 ret = NO_ERROR;
358 } else {
359 ret = NAME_NOT_FOUND;
360 }
361 break;
362
363 case BATTERY_PROP_CURRENT_NOW:
364 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700365 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700366 getIntField(mHealthdConfig->batteryCurrentNowPath);
367 ret = NO_ERROR;
368 } else {
369 ret = NAME_NOT_FOUND;
370 }
371 break;
372
Todd Poynorbc102112013-08-27 18:11:49 -0700373 case BATTERY_PROP_CURRENT_AVG:
374 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700375 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700376 getIntField(mHealthdConfig->batteryCurrentAvgPath);
377 ret = NO_ERROR;
378 } else {
379 ret = NAME_NOT_FOUND;
380 }
381 break;
382
Paul Lawrence347c8de2014-03-19 15:04:40 -0700383 case BATTERY_PROP_CAPACITY:
384 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700385 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700386 getIntField(mHealthdConfig->batteryCapacityPath);
387 ret = NO_ERROR;
388 } else {
389 ret = NAME_NOT_FOUND;
390 }
391 break;
392
Todd Poynor8f132af2014-05-08 17:15:45 -0700393 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700394 if (mHealthdConfig->energyCounter) {
395 ret = mHealthdConfig->energyCounter(&val->valueInt64);
396 } else {
397 ret = NAME_NOT_FOUND;
398 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700399 break;
400
Todd Poynorc133b712013-08-14 17:39:13 -0700401 default:
402 break;
403 }
404
Todd Poynorc133b712013-08-14 17:39:13 -0700405 return ret;
406}
407
Todd Poynor020369d2013-09-18 20:09:33 -0700408void BatteryMonitor::dumpState(int fd) {
409 int v;
410 char vs[128];
411
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700412 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d voltage_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700413 props.chargerAcOnline, props.chargerUsbOnline,
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700414 props.chargerWirelessOnline, props.maxChargingCurrent,
415 props.maxChargingVoltage);
Todd Poynor020369d2013-09-18 20:09:33 -0700416 write(fd, vs, strlen(vs));
417 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
418 props.batteryStatus, props.batteryHealth, props.batteryPresent);
419 write(fd, vs, strlen(vs));
420 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
421 props.batteryLevel, props.batteryVoltage,
422 props.batteryTemperature);
423 write(fd, vs, strlen(vs));
424
425 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
426 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
427 snprintf(vs, sizeof(vs), "current now: %d\n", v);
428 write(fd, vs, strlen(vs));
429 }
430
431 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
432 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
433 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
434 write(fd, vs, strlen(vs));
435 }
436
437 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
438 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
439 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
440 write(fd, vs, strlen(vs));
441 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700442
443 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
444 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
445 write(fd, vs, strlen(vs));
446 }
447
448 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
449 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
450 write(fd, vs, strlen(vs));
451 }
452
453 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
454 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
455 write(fd, vs, strlen(vs));
456 }
Todd Poynor020369d2013-09-18 20:09:33 -0700457}
458
Todd Poynorc7464c92013-09-10 12:40:00 -0700459void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700460 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700461 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700462
Todd Poynorf5d30122013-08-12 17:03:35 -0700463 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800464 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700465 if (dir == NULL) {
466 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
467 } else {
468 struct dirent* entry;
469
James Hawkins588a2ca2016-02-18 14:52:46 -0800470 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700471 const char* name = entry->d_name;
472
473 if (!strcmp(name, ".") || !strcmp(name, ".."))
474 continue;
475
Todd Poynor752faf22013-06-12 13:25:59 -0700476 // Look for "type" file in each subdirectory
477 path.clear();
478 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
479 switch(readPowerSupplyType(path)) {
480 case ANDROID_POWER_SUPPLY_TYPE_AC:
481 case ANDROID_POWER_SUPPLY_TYPE_USB:
482 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
483 path.clear();
484 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
485 if (access(path.string(), R_OK) == 0)
486 mChargerNames.add(String8(name));
487 break;
488
489 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700490 mBatteryDevicePresent = true;
491
Todd Poynorf5d30122013-08-12 17:03:35 -0700492 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700493 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700494 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
495 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700496 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700497 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700498 }
499
Todd Poynorf5d30122013-08-12 17:03:35 -0700500 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700501 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700502 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
503 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700504 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700505 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700506 }
507
Todd Poynorf5d30122013-08-12 17:03:35 -0700508 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
509 path.clear();
510 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
511 name);
512 if (access(path, R_OK) == 0)
513 mHealthdConfig->batteryPresentPath = path;
514 }
515
516 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
517 path.clear();
518 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
519 name);
520 if (access(path, R_OK) == 0)
521 mHealthdConfig->batteryCapacityPath = path;
522 }
523
524 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
525 path.clear();
526 path.appendFormat("%s/%s/voltage_now",
527 POWER_SUPPLY_SYSFS_PATH, name);
528 if (access(path, R_OK) == 0) {
529 mHealthdConfig->batteryVoltagePath = path;
530 } else {
531 path.clear();
532 path.appendFormat("%s/%s/batt_vol",
533 POWER_SUPPLY_SYSFS_PATH, name);
534 if (access(path, R_OK) == 0)
535 mHealthdConfig->batteryVoltagePath = path;
536 }
537 }
538
Ruchi Kandoicc338802015-08-24 13:01:16 -0700539 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
540 path.clear();
541 path.appendFormat("%s/%s/charge_full",
542 POWER_SUPPLY_SYSFS_PATH, name);
543 if (access(path, R_OK) == 0)
544 mHealthdConfig->batteryFullChargePath = path;
545 }
546
Todd Poynorf5d30122013-08-12 17:03:35 -0700547 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
548 path.clear();
549 path.appendFormat("%s/%s/current_now",
550 POWER_SUPPLY_SYSFS_PATH, name);
551 if (access(path, R_OK) == 0)
552 mHealthdConfig->batteryCurrentNowPath = path;
553 }
554
Ruchi Kandoicc338802015-08-24 13:01:16 -0700555 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
556 path.clear();
557 path.appendFormat("%s/%s/cycle_count",
558 POWER_SUPPLY_SYSFS_PATH, name);
559 if (access(path, R_OK) == 0)
560 mHealthdConfig->batteryCycleCountPath = path;
561 }
562
Todd Poynorbc102112013-08-27 18:11:49 -0700563 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
564 path.clear();
565 path.appendFormat("%s/%s/current_avg",
566 POWER_SUPPLY_SYSFS_PATH, name);
567 if (access(path, R_OK) == 0)
568 mHealthdConfig->batteryCurrentAvgPath = path;
569 }
570
Todd Poynorf5d30122013-08-12 17:03:35 -0700571 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
572 path.clear();
573 path.appendFormat("%s/%s/charge_counter",
574 POWER_SUPPLY_SYSFS_PATH, name);
575 if (access(path, R_OK) == 0)
576 mHealthdConfig->batteryChargeCounterPath = path;
577 }
578
579 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
580 path.clear();
581 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
582 name);
583 if (access(path, R_OK) == 0) {
584 mHealthdConfig->batteryTemperaturePath = path;
585 } else {
586 path.clear();
587 path.appendFormat("%s/%s/batt_temp",
588 POWER_SUPPLY_SYSFS_PATH, name);
589 if (access(path, R_OK) == 0)
590 mHealthdConfig->batteryTemperaturePath = path;
591 }
592 }
593
594 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
595 path.clear();
596 path.appendFormat("%s/%s/technology",
597 POWER_SUPPLY_SYSFS_PATH, name);
598 if (access(path, R_OK) == 0)
599 mHealthdConfig->batteryTechnologyPath = path;
600 }
601
Todd Poynor752faf22013-06-12 13:25:59 -0700602 break;
603
604 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
605 break;
606 }
607 }
Todd Poynor752faf22013-06-12 13:25:59 -0700608 }
609
Ian Pedowitz585ab652015-10-12 19:01:00 -0700610 // Typically the case for devices which do not have a battery and
611 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700612 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700613 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700614 hc->periodic_chores_interval_fast = -1;
615 hc->periodic_chores_interval_slow = -1;
Ruchi Kandoifabd4902016-02-29 13:13:39 -0800616 mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY;
617 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
618 mAlwaysPluggedDevice = true;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700619 } else {
620 if (mHealthdConfig->batteryStatusPath.isEmpty())
621 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
622 if (mHealthdConfig->batteryHealthPath.isEmpty())
623 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
624 if (mHealthdConfig->batteryPresentPath.isEmpty())
625 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
626 if (mHealthdConfig->batteryCapacityPath.isEmpty())
627 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
628 if (mHealthdConfig->batteryVoltagePath.isEmpty())
629 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
630 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
631 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
632 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
633 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700634 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700635 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
636 if (mHealthdConfig->batteryFullChargePath.isEmpty())
637 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
638 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
639 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700640 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700641
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700642 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
643 && strtol(pval, NULL, 10) != 0) {
644 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
645 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
646 }
Todd Poynor752faf22013-06-12 13:25:59 -0700647}
648
649}; // namespace android