blob: f641077f3d2d282bfc1098f1d35ceda6e9fb7f4d [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
Todd Poynor10b235e2013-08-07 15:25:14 -070019#include "healthd.h"
Todd Poynor752faf22013-06-12 13:25:59 -070020#include "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>
27#include <unistd.h>
28#include <batteryservice/BatteryService.h>
29#include <cutils/klog.h>
Todd Poynorc133b712013-08-14 17:39:13 -070030#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070031#include <utils/String8.h>
32#include <utils/Vector.h>
33
34#define POWER_SUPPLY_SUBSYSTEM "power_supply"
35#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
36
37namespace android {
38
39struct sysfsStringEnumMap {
40 char* s;
41 int val;
42};
43
44static int mapSysfsString(const char* str,
45 struct sysfsStringEnumMap map[]) {
46 for (int i = 0; map[i].s; i++)
47 if (!strcmp(str, map[i].s))
48 return map[i].val;
49
50 return -1;
51}
52
53int BatteryMonitor::getBatteryStatus(const char* status) {
54 int ret;
55 struct sysfsStringEnumMap batteryStatusMap[] = {
56 { "Unknown", BATTERY_STATUS_UNKNOWN },
57 { "Charging", BATTERY_STATUS_CHARGING },
58 { "Discharging", BATTERY_STATUS_DISCHARGING },
59 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
60 { "Full", BATTERY_STATUS_FULL },
61 { NULL, 0 },
62 };
63
64 ret = mapSysfsString(status, batteryStatusMap);
65 if (ret < 0) {
66 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
67 ret = BATTERY_STATUS_UNKNOWN;
68 }
69
70 return ret;
71}
72
73int BatteryMonitor::getBatteryHealth(const char* status) {
74 int ret;
75 struct sysfsStringEnumMap batteryHealthMap[] = {
76 { "Unknown", BATTERY_HEALTH_UNKNOWN },
77 { "Good", BATTERY_HEALTH_GOOD },
78 { "Overheat", BATTERY_HEALTH_OVERHEAT },
79 { "Dead", BATTERY_HEALTH_DEAD },
80 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
81 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
82 { "Cold", BATTERY_HEALTH_COLD },
83 { NULL, 0 },
84 };
85
86 ret = mapSysfsString(status, batteryHealthMap);
87 if (ret < 0) {
88 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
89 ret = BATTERY_HEALTH_UNKNOWN;
90 }
91
92 return ret;
93}
94
95int BatteryMonitor::readFromFile(const String8& path, char* buf, size_t size) {
96 char *cp = NULL;
97
98 if (path.isEmpty())
99 return -1;
100 int fd = open(path.string(), O_RDONLY, 0);
101 if (fd == -1) {
102 KLOG_ERROR(LOG_TAG, "Could not open '%s'\n", path.string());
103 return -1;
104 }
105
106 ssize_t count = TEMP_FAILURE_RETRY(read(fd, buf, size));
107 if (count > 0)
108 cp = (char *)memrchr(buf, '\n', count);
109
110 if (cp)
111 *cp = '\0';
112 else
113 buf[0] = '\0';
114
115 close(fd);
116 return count;
117}
118
119BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
120 const int SIZE = 128;
121 char buf[SIZE];
122 int length = readFromFile(path, buf, SIZE);
123 BatteryMonitor::PowerSupplyType ret;
124 struct sysfsStringEnumMap supplyTypeMap[] = {
125 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
126 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
127 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
128 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
129 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
130 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
131 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
132 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
133 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
134 { NULL, 0 },
135 };
136
137 if (length <= 0)
138 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
139
140 ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap);
141 if (ret < 0)
142 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
143
144 return ret;
145}
146
147bool BatteryMonitor::getBooleanField(const String8& path) {
148 const int SIZE = 16;
149 char buf[SIZE];
150
151 bool value = false;
152 if (readFromFile(path, buf, SIZE) > 0) {
153 if (buf[0] != '0') {
154 value = true;
155 }
156 }
157
158 return value;
159}
160
161int BatteryMonitor::getIntField(const String8& path) {
162 const int SIZE = 128;
163 char buf[SIZE];
164
165 int value = 0;
166 if (readFromFile(path, buf, SIZE) > 0) {
167 value = strtol(buf, NULL, 0);
168 }
169 return value;
170}
171
172bool BatteryMonitor::update(void) {
173 struct BatteryProperties props;
Todd Poynor10b235e2013-08-07 15:25:14 -0700174 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700175
176 props.chargerAcOnline = false;
177 props.chargerUsbOnline = false;
178 props.chargerWirelessOnline = false;
179 props.batteryStatus = BATTERY_STATUS_UNKNOWN;
180 props.batteryHealth = BATTERY_HEALTH_UNKNOWN;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700181 props.batteryCurrentNow = INT_MIN;
182 props.batteryChargeCounter = INT_MIN;
Todd Poynor752faf22013-06-12 13:25:59 -0700183
Todd Poynorf5d30122013-08-12 17:03:35 -0700184 if (!mHealthdConfig->batteryPresentPath.isEmpty())
185 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700186 else
187 props.batteryPresent = true;
188
Todd Poynorf5d30122013-08-12 17:03:35 -0700189 props.batteryLevel = getIntField(mHealthdConfig->batteryCapacityPath);
190 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700191
Todd Poynorf5d30122013-08-12 17:03:35 -0700192 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
193 props.batteryCurrentNow = getIntField(mHealthdConfig->batteryCurrentNowPath);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700194
Todd Poynorf5d30122013-08-12 17:03:35 -0700195 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
196 props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700197
Todd Poynorf5d30122013-08-12 17:03:35 -0700198 props.batteryTemperature = getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700199
200 const int SIZE = 128;
201 char buf[SIZE];
202 String8 btech;
203
Todd Poynorf5d30122013-08-12 17:03:35 -0700204 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700205 props.batteryStatus = getBatteryStatus(buf);
206
Todd Poynorf5d30122013-08-12 17:03:35 -0700207 if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700208 props.batteryHealth = getBatteryHealth(buf);
209
Todd Poynorf5d30122013-08-12 17:03:35 -0700210 if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700211 props.batteryTechnology = String8(buf);
212
213 unsigned int i;
214
215 for (i = 0; i < mChargerNames.size(); i++) {
216 String8 path;
217 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
218 mChargerNames[i].string());
219
220 if (readFromFile(path, buf, SIZE) > 0) {
221 if (buf[0] != '0') {
222 path.clear();
223 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
224 mChargerNames[i].string());
225 switch(readPowerSupplyType(path)) {
226 case ANDROID_POWER_SUPPLY_TYPE_AC:
227 props.chargerAcOnline = true;
228 break;
229 case ANDROID_POWER_SUPPLY_TYPE_USB:
230 props.chargerUsbOnline = true;
231 break;
232 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
233 props.chargerWirelessOnline = true;
234 break;
235 default:
236 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
237 mChargerNames[i].string());
238 }
239 }
240 }
241 }
242
Todd Poynor10b235e2013-08-07 15:25:14 -0700243 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700244
Todd Poynor10b235e2013-08-07 15:25:14 -0700245 if (logthis) {
246 char dmesgline[256];
247 snprintf(dmesgline, sizeof(dmesgline),
248 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
249 props.batteryLevel, props.batteryVoltage,
250 props.batteryTemperature < 0 ? "-" : "",
251 abs(props.batteryTemperature / 10),
252 abs(props.batteryTemperature % 10), props.batteryHealth,
253 props.batteryStatus);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700254
Todd Poynorf5d30122013-08-12 17:03:35 -0700255 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700256 char b[20];
257
Todd Poynor57fd0982013-08-09 12:22:07 -0700258 snprintf(b, sizeof(b), " c=%d", props.batteryCurrentNow / 1000);
Todd Poynor10b235e2013-08-07 15:25:14 -0700259 strlcat(dmesgline, b, sizeof(dmesgline));
260 }
261
262 KLOG_INFO(LOG_TAG, "%s chg=%s%s%s\n", dmesgline,
263 props.chargerAcOnline ? "a" : "",
264 props.chargerUsbOnline ? "u" : "",
265 props.chargerWirelessOnline ? "w" : "");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700266 }
267
Todd Poynorc7464c92013-09-10 12:40:00 -0700268 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700269 return props.chargerAcOnline | props.chargerUsbOnline |
270 props.chargerWirelessOnline;
271}
272
Todd Poynorc133b712013-08-14 17:39:13 -0700273status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
274 status_t ret = BAD_VALUE;
275
276 switch(id) {
277 case BATTERY_PROP_CHARGE_COUNTER:
278 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
279 val->valueInt =
280 getIntField(mHealthdConfig->batteryChargeCounterPath);
281 ret = NO_ERROR;
282 } else {
283 ret = NAME_NOT_FOUND;
284 }
285 break;
286
287 case BATTERY_PROP_CURRENT_NOW:
288 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
289 val->valueInt =
290 getIntField(mHealthdConfig->batteryCurrentNowPath);
291 ret = NO_ERROR;
292 } else {
293 ret = NAME_NOT_FOUND;
294 }
295 break;
296
Todd Poynorbc102112013-08-27 18:11:49 -0700297 case BATTERY_PROP_CURRENT_AVG:
298 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
299 val->valueInt =
300 getIntField(mHealthdConfig->batteryCurrentAvgPath);
301 ret = NO_ERROR;
302 } else {
303 ret = NAME_NOT_FOUND;
304 }
305 break;
306
Todd Poynorc133b712013-08-14 17:39:13 -0700307 default:
308 break;
309 }
310
311 if (ret != NO_ERROR)
312 val->valueInt = INT_MIN;
313
314 return ret;
315}
316
Todd Poynorc7464c92013-09-10 12:40:00 -0700317void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700318 String8 path;
319
Todd Poynorf5d30122013-08-12 17:03:35 -0700320 mHealthdConfig = hc;
Todd Poynor752faf22013-06-12 13:25:59 -0700321 DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
322 if (dir == NULL) {
323 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
324 } else {
325 struct dirent* entry;
326
327 while ((entry = readdir(dir))) {
328 const char* name = entry->d_name;
329
330 if (!strcmp(name, ".") || !strcmp(name, ".."))
331 continue;
332
333 char buf[20];
334 // Look for "type" file in each subdirectory
335 path.clear();
336 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
337 switch(readPowerSupplyType(path)) {
338 case ANDROID_POWER_SUPPLY_TYPE_AC:
339 case ANDROID_POWER_SUPPLY_TYPE_USB:
340 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
341 path.clear();
342 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
343 if (access(path.string(), R_OK) == 0)
344 mChargerNames.add(String8(name));
345 break;
346
347 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynorf5d30122013-08-12 17:03:35 -0700348 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700349 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700350 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
351 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700352 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700353 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700354 }
355
Todd Poynorf5d30122013-08-12 17:03:35 -0700356 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700357 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700358 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
359 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700360 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700361 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700362 }
363
Todd Poynorf5d30122013-08-12 17:03:35 -0700364 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
365 path.clear();
366 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
367 name);
368 if (access(path, R_OK) == 0)
369 mHealthdConfig->batteryPresentPath = path;
370 }
371
372 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
373 path.clear();
374 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
375 name);
376 if (access(path, R_OK) == 0)
377 mHealthdConfig->batteryCapacityPath = path;
378 }
379
380 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
381 path.clear();
382 path.appendFormat("%s/%s/voltage_now",
383 POWER_SUPPLY_SYSFS_PATH, name);
384 if (access(path, R_OK) == 0) {
385 mHealthdConfig->batteryVoltagePath = path;
386 } else {
387 path.clear();
388 path.appendFormat("%s/%s/batt_vol",
389 POWER_SUPPLY_SYSFS_PATH, name);
390 if (access(path, R_OK) == 0)
391 mHealthdConfig->batteryVoltagePath = path;
392 }
393 }
394
395 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
396 path.clear();
397 path.appendFormat("%s/%s/current_now",
398 POWER_SUPPLY_SYSFS_PATH, name);
399 if (access(path, R_OK) == 0)
400 mHealthdConfig->batteryCurrentNowPath = path;
401 }
402
Todd Poynorbc102112013-08-27 18:11:49 -0700403 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
404 path.clear();
405 path.appendFormat("%s/%s/current_avg",
406 POWER_SUPPLY_SYSFS_PATH, name);
407 if (access(path, R_OK) == 0)
408 mHealthdConfig->batteryCurrentAvgPath = path;
409 }
410
Todd Poynorf5d30122013-08-12 17:03:35 -0700411 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
412 path.clear();
413 path.appendFormat("%s/%s/charge_counter",
414 POWER_SUPPLY_SYSFS_PATH, name);
415 if (access(path, R_OK) == 0)
416 mHealthdConfig->batteryChargeCounterPath = path;
417 }
418
419 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
420 path.clear();
421 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
422 name);
423 if (access(path, R_OK) == 0) {
424 mHealthdConfig->batteryTemperaturePath = path;
425 } else {
426 path.clear();
427 path.appendFormat("%s/%s/batt_temp",
428 POWER_SUPPLY_SYSFS_PATH, name);
429 if (access(path, R_OK) == 0)
430 mHealthdConfig->batteryTemperaturePath = path;
431 }
432 }
433
434 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
435 path.clear();
436 path.appendFormat("%s/%s/technology",
437 POWER_SUPPLY_SYSFS_PATH, name);
438 if (access(path, R_OK) == 0)
439 mHealthdConfig->batteryTechnologyPath = path;
440 }
441
Todd Poynor752faf22013-06-12 13:25:59 -0700442 break;
443
444 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
445 break;
446 }
447 }
448 closedir(dir);
449 }
450
451 if (!mChargerNames.size())
452 KLOG_ERROR(LOG_TAG, "No charger supplies found\n");
Todd Poynorf5d30122013-08-12 17:03:35 -0700453 if (mHealthdConfig->batteryStatusPath.isEmpty())
Todd Poynor752faf22013-06-12 13:25:59 -0700454 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
Todd Poynorf5d30122013-08-12 17:03:35 -0700455 if (mHealthdConfig->batteryHealthPath.isEmpty())
Todd Poynora41611f2013-08-12 12:17:32 -0700456 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
Todd Poynorf5d30122013-08-12 17:03:35 -0700457 if (mHealthdConfig->batteryPresentPath.isEmpty())
Todd Poynor752faf22013-06-12 13:25:59 -0700458 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
Todd Poynorf5d30122013-08-12 17:03:35 -0700459 if (mHealthdConfig->batteryCapacityPath.isEmpty())
Todd Poynor752faf22013-06-12 13:25:59 -0700460 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
Todd Poynorf5d30122013-08-12 17:03:35 -0700461 if (mHealthdConfig->batteryVoltagePath.isEmpty())
Todd Poynor752faf22013-06-12 13:25:59 -0700462 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
Todd Poynorf5d30122013-08-12 17:03:35 -0700463 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
Todd Poynor752faf22013-06-12 13:25:59 -0700464 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
Todd Poynorf5d30122013-08-12 17:03:35 -0700465 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
Todd Poynor752faf22013-06-12 13:25:59 -0700466 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Todd Poynor752faf22013-06-12 13:25:59 -0700467}
468
469}; // namespace android