blob: ffd1d349abdf3b735d3e56fe1b5244d0d7e1d06b [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 Poynor2e227fb2013-09-30 15:36:44 -0700174 struct BatteryExtraProperties extraProps;
Todd Poynor10b235e2013-08-07 15:25:14 -0700175 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700176
177 props.chargerAcOnline = false;
178 props.chargerUsbOnline = false;
179 props.chargerWirelessOnline = false;
180 props.batteryStatus = BATTERY_STATUS_UNKNOWN;
181 props.batteryHealth = BATTERY_HEALTH_UNKNOWN;
Todd Poynor2e227fb2013-09-30 15:36:44 -0700182 extraProps.batteryCurrentNow = INT_MIN;
183 extraProps.batteryChargeCounter = INT_MIN;
Todd Poynor752faf22013-06-12 13:25:59 -0700184
Todd Poynorf5d30122013-08-12 17:03:35 -0700185 if (!mHealthdConfig->batteryPresentPath.isEmpty())
186 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700187 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700188 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700189
Todd Poynorf5d30122013-08-12 17:03:35 -0700190 props.batteryLevel = getIntField(mHealthdConfig->batteryCapacityPath);
191 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700192
Todd Poynorf5d30122013-08-12 17:03:35 -0700193 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
Todd Poynor2e227fb2013-09-30 15:36:44 -0700194 extraProps.batteryCurrentNow = getIntField(mHealthdConfig->batteryCurrentNowPath);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700195
Todd Poynor2e227fb2013-09-30 15:36:44 -0700196 /* temporary while dumpsys being reworked */
Todd Poynorf5d30122013-08-12 17:03:35 -0700197 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
Todd Poynor2e227fb2013-09-30 15:36:44 -0700198 extraProps.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700199
Todd Poynorf5d30122013-08-12 17:03:35 -0700200 props.batteryTemperature = getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700201
202 const int SIZE = 128;
203 char buf[SIZE];
204 String8 btech;
205
Todd Poynorf5d30122013-08-12 17:03:35 -0700206 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700207 props.batteryStatus = getBatteryStatus(buf);
208
Todd Poynorf5d30122013-08-12 17:03:35 -0700209 if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700210 props.batteryHealth = getBatteryHealth(buf);
211
Todd Poynorf5d30122013-08-12 17:03:35 -0700212 if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700213 props.batteryTechnology = String8(buf);
214
215 unsigned int i;
216
217 for (i = 0; i < mChargerNames.size(); i++) {
218 String8 path;
219 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
220 mChargerNames[i].string());
221
222 if (readFromFile(path, buf, SIZE) > 0) {
223 if (buf[0] != '0') {
224 path.clear();
225 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
226 mChargerNames[i].string());
227 switch(readPowerSupplyType(path)) {
228 case ANDROID_POWER_SUPPLY_TYPE_AC:
229 props.chargerAcOnline = true;
230 break;
231 case ANDROID_POWER_SUPPLY_TYPE_USB:
232 props.chargerUsbOnline = true;
233 break;
234 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
235 props.chargerWirelessOnline = true;
236 break;
237 default:
238 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
239 mChargerNames[i].string());
240 }
241 }
242 }
243 }
244
Todd Poynor2e227fb2013-09-30 15:36:44 -0700245 /* temporary while these are moved and dumpsys reworked */
246 props.batteryCurrentNow = extraProps.batteryCurrentNow;
247 props.batteryChargeCounter = extraProps.batteryChargeCounter;
248
Todd Poynor10b235e2013-08-07 15:25:14 -0700249 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700250
Todd Poynor10b235e2013-08-07 15:25:14 -0700251 if (logthis) {
252 char dmesgline[256];
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700253
254 if (props.batteryPresent)
255 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700256 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
257 props.batteryLevel, props.batteryVoltage,
258 props.batteryTemperature < 0 ? "-" : "",
259 abs(props.batteryTemperature / 10),
260 abs(props.batteryTemperature % 10), props.batteryHealth,
261 props.batteryStatus);
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700262 else
263 snprintf(dmesgline, sizeof(dmesgline),
264 "battery none");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700265
Todd Poynorf5d30122013-08-12 17:03:35 -0700266 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700267 char b[20];
268
Todd Poynor2e227fb2013-09-30 15:36:44 -0700269 snprintf(b, sizeof(b), " c=%d", extraProps.batteryCurrentNow / 1000);
Todd Poynor10b235e2013-08-07 15:25:14 -0700270 strlcat(dmesgline, b, sizeof(dmesgline));
271 }
272
273 KLOG_INFO(LOG_TAG, "%s chg=%s%s%s\n", dmesgline,
274 props.chargerAcOnline ? "a" : "",
275 props.chargerUsbOnline ? "u" : "",
276 props.chargerWirelessOnline ? "w" : "");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700277 }
278
Todd Poynorc7464c92013-09-10 12:40:00 -0700279 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700280 return props.chargerAcOnline | props.chargerUsbOnline |
281 props.chargerWirelessOnline;
282}
283
Todd Poynorc133b712013-08-14 17:39:13 -0700284status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
285 status_t ret = BAD_VALUE;
286
287 switch(id) {
288 case BATTERY_PROP_CHARGE_COUNTER:
289 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
290 val->valueInt =
291 getIntField(mHealthdConfig->batteryChargeCounterPath);
292 ret = NO_ERROR;
293 } else {
294 ret = NAME_NOT_FOUND;
295 }
296 break;
297
298 case BATTERY_PROP_CURRENT_NOW:
299 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
300 val->valueInt =
301 getIntField(mHealthdConfig->batteryCurrentNowPath);
302 ret = NO_ERROR;
303 } else {
304 ret = NAME_NOT_FOUND;
305 }
306 break;
307
Todd Poynorbc102112013-08-27 18:11:49 -0700308 case BATTERY_PROP_CURRENT_AVG:
309 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
310 val->valueInt =
311 getIntField(mHealthdConfig->batteryCurrentAvgPath);
312 ret = NO_ERROR;
313 } else {
314 ret = NAME_NOT_FOUND;
315 }
316 break;
317
Todd Poynorc133b712013-08-14 17:39:13 -0700318 default:
319 break;
320 }
321
322 if (ret != NO_ERROR)
323 val->valueInt = INT_MIN;
324
325 return ret;
326}
327
Todd Poynorc7464c92013-09-10 12:40:00 -0700328void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700329 String8 path;
330
Todd Poynorf5d30122013-08-12 17:03:35 -0700331 mHealthdConfig = hc;
Todd Poynor752faf22013-06-12 13:25:59 -0700332 DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
333 if (dir == NULL) {
334 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
335 } else {
336 struct dirent* entry;
337
338 while ((entry = readdir(dir))) {
339 const char* name = entry->d_name;
340
341 if (!strcmp(name, ".") || !strcmp(name, ".."))
342 continue;
343
344 char buf[20];
345 // Look for "type" file in each subdirectory
346 path.clear();
347 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
348 switch(readPowerSupplyType(path)) {
349 case ANDROID_POWER_SUPPLY_TYPE_AC:
350 case ANDROID_POWER_SUPPLY_TYPE_USB:
351 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
352 path.clear();
353 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
354 if (access(path.string(), R_OK) == 0)
355 mChargerNames.add(String8(name));
356 break;
357
358 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700359 mBatteryDevicePresent = true;
360
Todd Poynorf5d30122013-08-12 17:03:35 -0700361 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700362 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700363 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
364 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700365 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700366 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700367 }
368
Todd Poynorf5d30122013-08-12 17:03:35 -0700369 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700370 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700371 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
372 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700373 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700374 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700375 }
376
Todd Poynorf5d30122013-08-12 17:03:35 -0700377 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
378 path.clear();
379 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
380 name);
381 if (access(path, R_OK) == 0)
382 mHealthdConfig->batteryPresentPath = path;
383 }
384
385 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
386 path.clear();
387 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
388 name);
389 if (access(path, R_OK) == 0)
390 mHealthdConfig->batteryCapacityPath = path;
391 }
392
393 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
394 path.clear();
395 path.appendFormat("%s/%s/voltage_now",
396 POWER_SUPPLY_SYSFS_PATH, name);
397 if (access(path, R_OK) == 0) {
398 mHealthdConfig->batteryVoltagePath = path;
399 } else {
400 path.clear();
401 path.appendFormat("%s/%s/batt_vol",
402 POWER_SUPPLY_SYSFS_PATH, name);
403 if (access(path, R_OK) == 0)
404 mHealthdConfig->batteryVoltagePath = path;
405 }
406 }
407
408 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
409 path.clear();
410 path.appendFormat("%s/%s/current_now",
411 POWER_SUPPLY_SYSFS_PATH, name);
412 if (access(path, R_OK) == 0)
413 mHealthdConfig->batteryCurrentNowPath = path;
414 }
415
Todd Poynorbc102112013-08-27 18:11:49 -0700416 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
417 path.clear();
418 path.appendFormat("%s/%s/current_avg",
419 POWER_SUPPLY_SYSFS_PATH, name);
420 if (access(path, R_OK) == 0)
421 mHealthdConfig->batteryCurrentAvgPath = path;
422 }
423
Todd Poynorf5d30122013-08-12 17:03:35 -0700424 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
425 path.clear();
426 path.appendFormat("%s/%s/charge_counter",
427 POWER_SUPPLY_SYSFS_PATH, name);
428 if (access(path, R_OK) == 0)
429 mHealthdConfig->batteryChargeCounterPath = path;
430 }
431
432 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
433 path.clear();
434 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
435 name);
436 if (access(path, R_OK) == 0) {
437 mHealthdConfig->batteryTemperaturePath = path;
438 } else {
439 path.clear();
440 path.appendFormat("%s/%s/batt_temp",
441 POWER_SUPPLY_SYSFS_PATH, name);
442 if (access(path, R_OK) == 0)
443 mHealthdConfig->batteryTemperaturePath = path;
444 }
445 }
446
447 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
448 path.clear();
449 path.appendFormat("%s/%s/technology",
450 POWER_SUPPLY_SYSFS_PATH, name);
451 if (access(path, R_OK) == 0)
452 mHealthdConfig->batteryTechnologyPath = path;
453 }
454
Todd Poynor752faf22013-06-12 13:25:59 -0700455 break;
456
457 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
458 break;
459 }
460 }
461 closedir(dir);
462 }
463
464 if (!mChargerNames.size())
465 KLOG_ERROR(LOG_TAG, "No charger supplies found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700466 if (!mBatteryDevicePresent) {
467 KLOG_INFO(LOG_TAG, "No battery devices found\n");
468 hc->periodic_chores_interval_fast = -1;
469 hc->periodic_chores_interval_slow = -1;
470 } else {
471 if (mHealthdConfig->batteryStatusPath.isEmpty())
472 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
473 if (mHealthdConfig->batteryHealthPath.isEmpty())
474 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
475 if (mHealthdConfig->batteryPresentPath.isEmpty())
476 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
477 if (mHealthdConfig->batteryCapacityPath.isEmpty())
478 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
479 if (mHealthdConfig->batteryVoltagePath.isEmpty())
480 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
481 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
482 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
483 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
484 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
485 }
Todd Poynor752faf22013-06-12 13:25:59 -0700486}
487
488}; // namespace android