blob: 9e168e9e5b77eb637b0b68b73f1483493a434a66 [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>
Thierry Strudelf73de6f2019-01-11 17:09:20 -080029
30#include <algorithm>
James Hawkins588a2ca2016-02-18 14:52:46 -080031#include <memory>
Yifan Hong1d4368b2019-10-07 11:18:04 -070032#include <optional>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070033
Michael Scott3217c5c2016-06-05 11:20:13 -070034#include <android-base/file.h>
Elliott Hughesda46b392016-10-11 17:09:00 -070035#include <android-base/parseint.h>
Michael Scott3217c5c2016-06-05 11:20:13 -070036#include <android-base/strings.h>
Yifan Hong1d4368b2019-10-07 11:18:04 -070037#include <android/hardware/health/2.1/types.h>
Todd Poynor752faf22013-06-12 13:25:59 -070038#include <batteryservice/BatteryService.h>
39#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070040#include <cutils/properties.h>
Todd Poynorc133b712013-08-14 17:39:13 -070041#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070042#include <utils/String8.h>
43#include <utils/Vector.h>
44
45#define POWER_SUPPLY_SUBSYSTEM "power_supply"
46#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070047#define FAKE_BATTERY_CAPACITY 42
48#define FAKE_BATTERY_TEMPERATURE 424
Ruchi Kandoi5c09ec12016-02-25 16:19:30 -080049#define MILLION 1.0e6
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070050#define DEFAULT_VBUS_VOLTAGE 5000000
Todd Poynor752faf22013-06-12 13:25:59 -070051
Yifan Hong1d4368b2019-10-07 11:18:04 -070052using HealthInfo_1_0 = android::hardware::health::V1_0::HealthInfo;
53using HealthInfo_2_0 = android::hardware::health::V2_0::HealthInfo;
54using HealthInfo_2_1 = android::hardware::health::V2_1::HealthInfo;
55using android::hardware::health::V1_0::BatteryHealth;
56using android::hardware::health::V1_0::BatteryStatus;
Yifan Hong35cb0832019-10-07 13:58:29 -070057using android::hardware::health::V2_1::BatteryCapacityLevel;
Yifan Hong1d4368b2019-10-07 11:18:04 -070058
Todd Poynor752faf22013-06-12 13:25:59 -070059namespace android {
60
Yifan Hong1d4368b2019-10-07 11:18:04 -070061template <typename T>
62struct SysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -070063 const char* s;
Yifan Hong1d4368b2019-10-07 11:18:04 -070064 T val;
Todd Poynor752faf22013-06-12 13:25:59 -070065};
66
Yifan Hong1d4368b2019-10-07 11:18:04 -070067template <typename T>
68static std::optional<T> mapSysfsString(const char* str, SysfsStringEnumMap<T> map[]) {
Todd Poynor752faf22013-06-12 13:25:59 -070069 for (int i = 0; map[i].s; i++)
70 if (!strcmp(str, map[i].s))
71 return map[i].val;
72
Yifan Hong1d4368b2019-10-07 11:18:04 -070073 return std::nullopt;
Yabin Cuidb04a492016-02-16 17:19:23 -080074}
75
Todd Poynore030a102018-01-19 14:03:59 -080076BatteryMonitor::BatteryMonitor()
77 : mHealthdConfig(nullptr),
78 mBatteryDevicePresent(false),
79 mBatteryFixedCapacity(0),
Yifan Hong1d4368b2019-10-07 11:18:04 -070080 mBatteryFixedTemperature(0),
81 mHealthInfo(std::make_unique<HealthInfo_2_1>()) {}
82
83BatteryMonitor::~BatteryMonitor() {}
84
85const HealthInfo_1_0& BatteryMonitor::getHealthInfo_1_0() const {
86 return getHealthInfo_2_0().legacy;
Yabin Cuidb04a492016-02-16 17:19:23 -080087}
88
Yifan Hong1d4368b2019-10-07 11:18:04 -070089const HealthInfo_2_0& BatteryMonitor::getHealthInfo_2_0() const {
90 return getHealthInfo_2_1().legacy;
Hridya Valsaraju7fa72252018-01-12 17:44:33 -080091}
92
Yifan Hong1d4368b2019-10-07 11:18:04 -070093const HealthInfo_2_1& BatteryMonitor::getHealthInfo_2_1() const {
94 return *mHealthInfo;
95}
96
97BatteryStatus getBatteryStatus(const char* status) {
98 static SysfsStringEnumMap<BatteryStatus> batteryStatusMap[] = {
99 {"Unknown", BatteryStatus::UNKNOWN},
100 {"Charging", BatteryStatus::CHARGING},
101 {"Discharging", BatteryStatus::DISCHARGING},
102 {"Not charging", BatteryStatus::NOT_CHARGING},
103 {"Full", BatteryStatus::FULL},
104 {NULL, BatteryStatus::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700105 };
106
Yifan Hong1d4368b2019-10-07 11:18:04 -0700107 auto ret = mapSysfsString(status, batteryStatusMap);
108 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700109 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700110 *ret = BatteryStatus::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700111 }
112
Yifan Hong1d4368b2019-10-07 11:18:04 -0700113 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700114}
115
Yifan Hong1d4368b2019-10-07 11:18:04 -0700116BatteryHealth getBatteryHealth(const char* status) {
117 static SysfsStringEnumMap<BatteryHealth> batteryHealthMap[] = {
118 {"Unknown", BatteryHealth::UNKNOWN},
119 {"Good", BatteryHealth::GOOD},
120 {"Overheat", BatteryHealth::OVERHEAT},
121 {"Dead", BatteryHealth::DEAD},
122 {"Over voltage", BatteryHealth::OVER_VOLTAGE},
123 {"Unspecified failure", BatteryHealth::UNSPECIFIED_FAILURE},
124 {"Cold", BatteryHealth::COLD},
125 // battery health values from JEITA spec
126 {"Warm", BatteryHealth::GOOD},
127 {"Cool", BatteryHealth::GOOD},
128 {"Hot", BatteryHealth::OVERHEAT},
129 {NULL, BatteryHealth::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700130 };
131
Yifan Hong1d4368b2019-10-07 11:18:04 -0700132 auto ret = mapSysfsString(status, batteryHealthMap);
133 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700134 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700135 *ret = BatteryHealth::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700136 }
137
Yifan Hong1d4368b2019-10-07 11:18:04 -0700138 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700139}
140
Michael Scott3217c5c2016-06-05 11:20:13 -0700141int BatteryMonitor::readFromFile(const String8& path, std::string* buf) {
Steven Moreland2aac3352017-03-10 22:31:08 -0800142 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700143 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700144 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700145 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700146}
147
148BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700149 static SysfsStringEnumMap<int> 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},
156 {"USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC},
157 {"USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC},
158 {"USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC},
159 {"USB_C", ANDROID_POWER_SUPPLY_TYPE_AC},
160 {"USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC},
161 {"USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB},
162 {"Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS},
163 {NULL, 0},
Todd Poynor752faf22013-06-12 13:25:59 -0700164 };
Yifan Hong1d4368b2019-10-07 11:18:04 -0700165 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700166
Michael Scott3217c5c2016-06-05 11:20:13 -0700167 if (readFromFile(path, &buf) <= 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700168 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
169
Yifan Hong1d4368b2019-10-07 11:18:04 -0700170 auto ret = mapSysfsString(buf.c_str(), supplyTypeMap);
Johan Redestig32828612016-02-03 13:45:54 +0100171 if (ret < 0) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700172 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Yifan Hong1d4368b2019-10-07 11:18:04 -0700173 *ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100174 }
Todd Poynor752faf22013-06-12 13:25:59 -0700175
Yifan Hong1d4368b2019-10-07 11:18:04 -0700176 return static_cast<BatteryMonitor::PowerSupplyType>(*ret);
Todd Poynor752faf22013-06-12 13:25:59 -0700177}
178
179bool BatteryMonitor::getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700180 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700181 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700182
183 if (readFromFile(path, &buf) > 0)
184 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700185 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700186
187 return value;
188}
189
190int BatteryMonitor::getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700191 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700192 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700193
194 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700195 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700196
Todd Poynor752faf22013-06-12 13:25:59 -0700197 return value;
198}
199
Yifan Hong1353e702019-10-07 10:41:30 -0700200void BatteryMonitor::updateValues(void) {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700201 *mHealthInfo = HealthInfo_2_1{};
202
203 HealthInfo_1_0& props = mHealthInfo->legacy.legacy;
Todd Poynor752faf22013-06-12 13:25:59 -0700204
Todd Poynorf5d30122013-08-12 17:03:35 -0700205 if (!mHealthdConfig->batteryPresentPath.isEmpty())
206 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700207 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700208 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700209
Todd Poynor3db03a52014-05-21 16:28:13 -0700210 props.batteryLevel = mBatteryFixedCapacity ?
211 mBatteryFixedCapacity :
212 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700213 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700214
Ruchi Kandoicc338802015-08-24 13:01:16 -0700215 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
216 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
217
218 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
219 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
220
221 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
222 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
223
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700224 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
225 props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
226
Yifan Hong35cb0832019-10-07 13:58:29 -0700227 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty())
228 mHealthInfo->legacy.batteryCurrentAverage =
229 getIntField(mHealthdConfig->batteryCurrentAvgPath);
230
231 // TODO(b/142260281): Retrieve these values correctly.
232 mHealthInfo->batteryCapacityLevel = BatteryCapacityLevel::UNKNOWN;
233 mHealthInfo->batteryChargeTimeToFullNowSeconds = 0;
234 mHealthInfo->batteryFullCapacityUah = props.batteryFullCharge;
235
Todd Poynor3db03a52014-05-21 16:28:13 -0700236 props.batteryTemperature = mBatteryFixedTemperature ?
237 mBatteryFixedTemperature :
238 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700239
Michael Scott3217c5c2016-06-05 11:20:13 -0700240 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700241
Michael Scott3217c5c2016-06-05 11:20:13 -0700242 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
243 props.batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700244
Michael Scott3217c5c2016-06-05 11:20:13 -0700245 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
246 props.batteryHealth = getBatteryHealth(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700247
Michael Scott3217c5c2016-06-05 11:20:13 -0700248 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
249 props.batteryTechnology = String8(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700250
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700251 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700252
ShevT9d98a6a2018-07-26 11:47:47 +0300253 for (size_t i = 0; i < mChargerNames.size(); i++) {
Todd Poynor752faf22013-06-12 13:25:59 -0700254 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 }
Yifan Hong1353e702019-10-07 10:41:30 -0700298}
Todd Poynor752faf22013-06-12 13:25:59 -0700299
Yifan Hong1353e702019-10-07 10:41:30 -0700300void BatteryMonitor::logValues(void) {
301 char dmesgline[256];
302 size_t len;
Yifan Hong1d4368b2019-10-07 11:18:04 -0700303 const HealthInfo_1_0& props = mHealthInfo->legacy.legacy;
Yifan Hong1353e702019-10-07 10:41:30 -0700304 if (props.batteryPresent) {
305 snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
306 props.batteryLevel, props.batteryVoltage, props.batteryTemperature < 0 ? "-" : "",
307 abs(props.batteryTemperature / 10), abs(props.batteryTemperature % 10),
308 props.batteryHealth, props.batteryStatus);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700309
Yifan Hong1353e702019-10-07 10:41:30 -0700310 len = strlen(dmesgline);
311 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
312 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d",
313 props.batteryCurrent);
Todd Poynor10b235e2013-08-07 15:25:14 -0700314 }
315
Yifan Hong1353e702019-10-07 10:41:30 -0700316 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
317 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d",
318 props.batteryFullCharge);
319 }
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700320
Yifan Hong1353e702019-10-07 10:41:30 -0700321 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
322 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d",
323 props.batteryCycleCount);
324 }
325 } else {
326 len = snprintf(dmesgline, sizeof(dmesgline), "battery none");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700327 }
328
Yifan Hong1353e702019-10-07 10:41:30 -0700329 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
330 props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "",
331 props.chargerWirelessOnline ? "w" : "");
332
333 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
334}
335
336bool BatteryMonitor::isChargerOnline() {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700337 const HealthInfo_1_0& props = mHealthInfo->legacy.legacy;
Todd Poynor752faf22013-06-12 13:25:59 -0700338 return props.chargerAcOnline | props.chargerUsbOnline |
339 props.chargerWirelessOnline;
340}
341
Yabin Cuiaedf6032016-02-19 18:03:23 -0800342int BatteryMonitor::getChargeStatus() {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700343 BatteryStatus result = BatteryStatus::UNKNOWN;
Yabin Cuiaedf6032016-02-19 18:03:23 -0800344 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700345 std::string buf;
346 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
347 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800348 }
Yifan Hong1d4368b2019-10-07 11:18:04 -0700349 return static_cast<int>(result);
Yabin Cuiaedf6032016-02-19 18:03:23 -0800350}
351
Todd Poynorc133b712013-08-14 17:39:13 -0700352status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
353 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800354 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700355
Todd Poynor8f132af2014-05-08 17:15:45 -0700356 val->valueInt64 = LONG_MIN;
357
Todd Poynorc133b712013-08-14 17:39:13 -0700358 switch(id) {
359 case BATTERY_PROP_CHARGE_COUNTER:
360 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700361 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700362 getIntField(mHealthdConfig->batteryChargeCounterPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700363 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700364 } else {
365 ret = NAME_NOT_FOUND;
366 }
367 break;
368
369 case BATTERY_PROP_CURRENT_NOW:
370 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700371 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700372 getIntField(mHealthdConfig->batteryCurrentNowPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700373 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700374 } else {
375 ret = NAME_NOT_FOUND;
376 }
377 break;
378
Todd Poynorbc102112013-08-27 18:11:49 -0700379 case BATTERY_PROP_CURRENT_AVG:
380 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700381 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700382 getIntField(mHealthdConfig->batteryCurrentAvgPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700383 ret = OK;
Todd Poynorbc102112013-08-27 18:11:49 -0700384 } else {
385 ret = NAME_NOT_FOUND;
386 }
387 break;
388
Paul Lawrence347c8de2014-03-19 15:04:40 -0700389 case BATTERY_PROP_CAPACITY:
390 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700391 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700392 getIntField(mHealthdConfig->batteryCapacityPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700393 ret = OK;
Paul Lawrence347c8de2014-03-19 15:04:40 -0700394 } else {
395 ret = NAME_NOT_FOUND;
396 }
397 break;
398
Todd Poynor8f132af2014-05-08 17:15:45 -0700399 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700400 if (mHealthdConfig->energyCounter) {
401 ret = mHealthdConfig->energyCounter(&val->valueInt64);
402 } else {
403 ret = NAME_NOT_FOUND;
404 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700405 break;
406
Jin Qian72adf112017-02-02 17:31:13 -0800407 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynore030a102018-01-19 14:03:59 -0800408 val->valueInt64 = getChargeStatus();
Elliott Hughes643268f2018-10-08 11:10:11 -0700409 ret = OK;
Jin Qian72adf112017-02-02 17:31:13 -0800410 break;
411
Todd Poynorc133b712013-08-14 17:39:13 -0700412 default:
413 break;
414 }
415
Todd Poynorc133b712013-08-14 17:39:13 -0700416 return ret;
417}
418
Todd Poynor020369d2013-09-18 20:09:33 -0700419void BatteryMonitor::dumpState(int fd) {
420 int v;
421 char vs[128];
Yifan Hong1d4368b2019-10-07 11:18:04 -0700422 const HealthInfo_1_0& props = mHealthInfo->legacy.legacy;
Todd Poynor020369d2013-09-18 20:09:33 -0700423
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700424 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d voltage_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700425 props.chargerAcOnline, props.chargerUsbOnline,
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700426 props.chargerWirelessOnline, props.maxChargingCurrent,
427 props.maxChargingVoltage);
Todd Poynor020369d2013-09-18 20:09:33 -0700428 write(fd, vs, strlen(vs));
429 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
430 props.batteryStatus, props.batteryHealth, props.batteryPresent);
431 write(fd, vs, strlen(vs));
432 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
433 props.batteryLevel, props.batteryVoltage,
434 props.batteryTemperature);
435 write(fd, vs, strlen(vs));
436
437 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
438 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
439 snprintf(vs, sizeof(vs), "current now: %d\n", v);
440 write(fd, vs, strlen(vs));
441 }
442
443 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
444 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
445 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
446 write(fd, vs, strlen(vs));
447 }
448
449 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
450 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
451 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
452 write(fd, vs, strlen(vs));
453 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700454
455 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
456 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
457 write(fd, vs, strlen(vs));
458 }
459
460 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
461 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
462 write(fd, vs, strlen(vs));
463 }
464
465 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
466 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
467 write(fd, vs, strlen(vs));
468 }
Todd Poynor020369d2013-09-18 20:09:33 -0700469}
470
Todd Poynorc7464c92013-09-10 12:40:00 -0700471void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700472 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700473 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700474
Todd Poynorf5d30122013-08-12 17:03:35 -0700475 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800476 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700477 if (dir == NULL) {
478 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
479 } else {
480 struct dirent* entry;
481
James Hawkins588a2ca2016-02-18 14:52:46 -0800482 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700483 const char* name = entry->d_name;
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800484 std::vector<String8>::iterator itIgnoreName;
Todd Poynor752faf22013-06-12 13:25:59 -0700485
486 if (!strcmp(name, ".") || !strcmp(name, ".."))
487 continue;
488
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800489 itIgnoreName = find(hc->ignorePowerSupplyNames.begin(),
490 hc->ignorePowerSupplyNames.end(), String8(name));
491 if (itIgnoreName != hc->ignorePowerSupplyNames.end())
492 continue;
493
Todd Poynor752faf22013-06-12 13:25:59 -0700494 // Look for "type" file in each subdirectory
495 path.clear();
496 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
497 switch(readPowerSupplyType(path)) {
498 case ANDROID_POWER_SUPPLY_TYPE_AC:
499 case ANDROID_POWER_SUPPLY_TYPE_USB:
500 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
501 path.clear();
502 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
503 if (access(path.string(), R_OK) == 0)
504 mChargerNames.add(String8(name));
505 break;
506
507 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700508 mBatteryDevicePresent = true;
509
Todd Poynorf5d30122013-08-12 17:03:35 -0700510 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700511 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700512 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
513 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700514 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700515 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700516 }
517
Todd Poynorf5d30122013-08-12 17:03:35 -0700518 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700519 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700520 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
521 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700522 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700523 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700524 }
525
Todd Poynorf5d30122013-08-12 17:03:35 -0700526 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
527 path.clear();
528 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
529 name);
530 if (access(path, R_OK) == 0)
531 mHealthdConfig->batteryPresentPath = path;
532 }
533
534 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
535 path.clear();
536 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
537 name);
538 if (access(path, R_OK) == 0)
539 mHealthdConfig->batteryCapacityPath = path;
540 }
541
542 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
543 path.clear();
544 path.appendFormat("%s/%s/voltage_now",
545 POWER_SUPPLY_SYSFS_PATH, name);
546 if (access(path, R_OK) == 0) {
547 mHealthdConfig->batteryVoltagePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700548 }
549 }
550
Ruchi Kandoicc338802015-08-24 13:01:16 -0700551 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
552 path.clear();
553 path.appendFormat("%s/%s/charge_full",
554 POWER_SUPPLY_SYSFS_PATH, name);
555 if (access(path, R_OK) == 0)
556 mHealthdConfig->batteryFullChargePath = path;
557 }
558
Todd Poynorf5d30122013-08-12 17:03:35 -0700559 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
560 path.clear();
561 path.appendFormat("%s/%s/current_now",
562 POWER_SUPPLY_SYSFS_PATH, name);
563 if (access(path, R_OK) == 0)
564 mHealthdConfig->batteryCurrentNowPath = path;
565 }
566
Ruchi Kandoicc338802015-08-24 13:01:16 -0700567 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
568 path.clear();
569 path.appendFormat("%s/%s/cycle_count",
570 POWER_SUPPLY_SYSFS_PATH, name);
571 if (access(path, R_OK) == 0)
572 mHealthdConfig->batteryCycleCountPath = path;
573 }
574
Todd Poynorbc102112013-08-27 18:11:49 -0700575 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
576 path.clear();
577 path.appendFormat("%s/%s/current_avg",
578 POWER_SUPPLY_SYSFS_PATH, name);
579 if (access(path, R_OK) == 0)
580 mHealthdConfig->batteryCurrentAvgPath = path;
581 }
582
Todd Poynorf5d30122013-08-12 17:03:35 -0700583 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
584 path.clear();
585 path.appendFormat("%s/%s/charge_counter",
586 POWER_SUPPLY_SYSFS_PATH, name);
587 if (access(path, R_OK) == 0)
588 mHealthdConfig->batteryChargeCounterPath = path;
589 }
590
591 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
592 path.clear();
593 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
594 name);
595 if (access(path, R_OK) == 0) {
596 mHealthdConfig->batteryTemperaturePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700597 }
598 }
599
600 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
601 path.clear();
602 path.appendFormat("%s/%s/technology",
603 POWER_SUPPLY_SYSFS_PATH, name);
604 if (access(path, R_OK) == 0)
605 mHealthdConfig->batteryTechnologyPath = path;
606 }
607
Todd Poynor752faf22013-06-12 13:25:59 -0700608 break;
609
610 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
611 break;
612 }
613 }
Todd Poynor752faf22013-06-12 13:25:59 -0700614 }
615
Ian Pedowitz585ab652015-10-12 19:01:00 -0700616 // Typically the case for devices which do not have a battery and
617 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700618 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700619 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700620 hc->periodic_chores_interval_fast = -1;
621 hc->periodic_chores_interval_slow = -1;
622 } else {
623 if (mHealthdConfig->batteryStatusPath.isEmpty())
624 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
625 if (mHealthdConfig->batteryHealthPath.isEmpty())
626 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
627 if (mHealthdConfig->batteryPresentPath.isEmpty())
628 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
629 if (mHealthdConfig->batteryCapacityPath.isEmpty())
630 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
631 if (mHealthdConfig->batteryVoltagePath.isEmpty())
632 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
633 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
634 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
635 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
636 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700637 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700638 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
639 if (mHealthdConfig->batteryFullChargePath.isEmpty())
640 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
641 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
642 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700643 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700644
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700645 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
646 && strtol(pval, NULL, 10) != 0) {
647 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
648 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
649 }
Todd Poynor752faf22013-06-12 13:25:59 -0700650}
651
652}; // namespace android