Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 19 | #include "healthd.h" |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 20 | #include "BatteryMonitor.h" |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 21 | |
| 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 Poynor | e14b37e | 2014-05-20 13:54:40 -0700 | [diff] [blame] | 30 | #include <sys/types.h> |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 31 | #include <utils/Errors.h> |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 32 | #include <utils/String8.h> |
| 33 | #include <utils/Vector.h> |
| 34 | |
| 35 | #define POWER_SUPPLY_SUBSYSTEM "power_supply" |
| 36 | #define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM |
| 37 | |
| 38 | namespace android { |
| 39 | |
| 40 | struct sysfsStringEnumMap { |
Mark Salyzyn | 6f5b47f | 2014-05-15 15:00:59 -0700 | [diff] [blame] | 41 | const char* s; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 42 | int val; |
| 43 | }; |
| 44 | |
| 45 | static int mapSysfsString(const char* str, |
| 46 | struct sysfsStringEnumMap map[]) { |
| 47 | for (int i = 0; map[i].s; i++) |
| 48 | if (!strcmp(str, map[i].s)) |
| 49 | return map[i].val; |
| 50 | |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | int BatteryMonitor::getBatteryStatus(const char* status) { |
| 55 | int ret; |
| 56 | struct sysfsStringEnumMap batteryStatusMap[] = { |
| 57 | { "Unknown", BATTERY_STATUS_UNKNOWN }, |
| 58 | { "Charging", BATTERY_STATUS_CHARGING }, |
| 59 | { "Discharging", BATTERY_STATUS_DISCHARGING }, |
| 60 | { "Not charging", BATTERY_STATUS_NOT_CHARGING }, |
| 61 | { "Full", BATTERY_STATUS_FULL }, |
| 62 | { NULL, 0 }, |
| 63 | }; |
| 64 | |
| 65 | ret = mapSysfsString(status, batteryStatusMap); |
| 66 | if (ret < 0) { |
| 67 | KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status); |
| 68 | ret = BATTERY_STATUS_UNKNOWN; |
| 69 | } |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | int BatteryMonitor::getBatteryHealth(const char* status) { |
| 75 | int ret; |
| 76 | struct sysfsStringEnumMap batteryHealthMap[] = { |
| 77 | { "Unknown", BATTERY_HEALTH_UNKNOWN }, |
| 78 | { "Good", BATTERY_HEALTH_GOOD }, |
| 79 | { "Overheat", BATTERY_HEALTH_OVERHEAT }, |
| 80 | { "Dead", BATTERY_HEALTH_DEAD }, |
| 81 | { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE }, |
| 82 | { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE }, |
| 83 | { "Cold", BATTERY_HEALTH_COLD }, |
| 84 | { NULL, 0 }, |
| 85 | }; |
| 86 | |
| 87 | ret = mapSysfsString(status, batteryHealthMap); |
| 88 | if (ret < 0) { |
| 89 | KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status); |
| 90 | ret = BATTERY_HEALTH_UNKNOWN; |
| 91 | } |
| 92 | |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | int BatteryMonitor::readFromFile(const String8& path, char* buf, size_t size) { |
| 97 | char *cp = NULL; |
| 98 | |
| 99 | if (path.isEmpty()) |
| 100 | return -1; |
| 101 | int fd = open(path.string(), O_RDONLY, 0); |
| 102 | if (fd == -1) { |
| 103 | KLOG_ERROR(LOG_TAG, "Could not open '%s'\n", path.string()); |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | ssize_t count = TEMP_FAILURE_RETRY(read(fd, buf, size)); |
| 108 | if (count > 0) |
| 109 | cp = (char *)memrchr(buf, '\n', count); |
| 110 | |
| 111 | if (cp) |
| 112 | *cp = '\0'; |
| 113 | else |
| 114 | buf[0] = '\0'; |
| 115 | |
| 116 | close(fd); |
| 117 | return count; |
| 118 | } |
| 119 | |
| 120 | BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) { |
| 121 | const int SIZE = 128; |
| 122 | char buf[SIZE]; |
| 123 | int length = readFromFile(path, buf, SIZE); |
| 124 | BatteryMonitor::PowerSupplyType ret; |
| 125 | struct sysfsStringEnumMap supplyTypeMap[] = { |
| 126 | { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN }, |
| 127 | { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY }, |
| 128 | { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC }, |
| 129 | { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC }, |
| 130 | { "USB", ANDROID_POWER_SUPPLY_TYPE_USB }, |
| 131 | { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC }, |
| 132 | { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC }, |
| 133 | { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC }, |
| 134 | { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS }, |
| 135 | { NULL, 0 }, |
| 136 | }; |
| 137 | |
| 138 | if (length <= 0) |
| 139 | return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; |
| 140 | |
| 141 | ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap); |
| 142 | if (ret < 0) |
| 143 | ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; |
| 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | bool BatteryMonitor::getBooleanField(const String8& path) { |
| 149 | const int SIZE = 16; |
| 150 | char buf[SIZE]; |
| 151 | |
| 152 | bool value = false; |
| 153 | if (readFromFile(path, buf, SIZE) > 0) { |
| 154 | if (buf[0] != '0') { |
| 155 | value = true; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return value; |
| 160 | } |
| 161 | |
| 162 | int BatteryMonitor::getIntField(const String8& path) { |
| 163 | const int SIZE = 128; |
| 164 | char buf[SIZE]; |
| 165 | |
| 166 | int value = 0; |
| 167 | if (readFromFile(path, buf, SIZE) > 0) { |
| 168 | value = strtol(buf, NULL, 0); |
| 169 | } |
| 170 | return value; |
| 171 | } |
| 172 | |
| 173 | bool BatteryMonitor::update(void) { |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 174 | bool logthis; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 175 | |
| 176 | props.chargerAcOnline = false; |
| 177 | props.chargerUsbOnline = false; |
| 178 | props.chargerWirelessOnline = false; |
| 179 | props.batteryStatus = BATTERY_STATUS_UNKNOWN; |
| 180 | props.batteryHealth = BATTERY_HEALTH_UNKNOWN; |
| 181 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 182 | if (!mHealthdConfig->batteryPresentPath.isEmpty()) |
| 183 | props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 184 | else |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 185 | props.batteryPresent = mBatteryDevicePresent; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 186 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 187 | props.batteryLevel = getIntField(mHealthdConfig->batteryCapacityPath); |
| 188 | props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000; |
Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 189 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 190 | props.batteryTemperature = getIntField(mHealthdConfig->batteryTemperaturePath); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 191 | |
| 192 | const int SIZE = 128; |
| 193 | char buf[SIZE]; |
| 194 | String8 btech; |
| 195 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 196 | if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0) |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 197 | props.batteryStatus = getBatteryStatus(buf); |
| 198 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 199 | if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0) |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 200 | props.batteryHealth = getBatteryHealth(buf); |
| 201 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 202 | if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0) |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 203 | props.batteryTechnology = String8(buf); |
| 204 | |
| 205 | unsigned int i; |
| 206 | |
| 207 | for (i = 0; i < mChargerNames.size(); i++) { |
| 208 | String8 path; |
| 209 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, |
| 210 | mChargerNames[i].string()); |
| 211 | |
| 212 | if (readFromFile(path, buf, SIZE) > 0) { |
| 213 | if (buf[0] != '0') { |
| 214 | path.clear(); |
| 215 | path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, |
| 216 | mChargerNames[i].string()); |
| 217 | switch(readPowerSupplyType(path)) { |
| 218 | case ANDROID_POWER_SUPPLY_TYPE_AC: |
| 219 | props.chargerAcOnline = true; |
| 220 | break; |
| 221 | case ANDROID_POWER_SUPPLY_TYPE_USB: |
| 222 | props.chargerUsbOnline = true; |
| 223 | break; |
| 224 | case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: |
| 225 | props.chargerWirelessOnline = true; |
| 226 | break; |
| 227 | default: |
| 228 | KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n", |
| 229 | mChargerNames[i].string()); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 235 | logthis = !healthd_board_battery_update(&props); |
Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 236 | |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 237 | if (logthis) { |
| 238 | char dmesgline[256]; |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 239 | |
Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 240 | if (props.batteryPresent) { |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 241 | snprintf(dmesgline, sizeof(dmesgline), |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 242 | "battery l=%d v=%d t=%s%d.%d h=%d st=%d", |
| 243 | props.batteryLevel, props.batteryVoltage, |
| 244 | props.batteryTemperature < 0 ? "-" : "", |
| 245 | abs(props.batteryTemperature / 10), |
| 246 | abs(props.batteryTemperature % 10), props.batteryHealth, |
| 247 | props.batteryStatus); |
Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 248 | |
| 249 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
| 250 | int c = getIntField(mHealthdConfig->batteryCurrentNowPath); |
| 251 | char b[20]; |
| 252 | |
| 253 | snprintf(b, sizeof(b), " c=%d", c / 1000); |
| 254 | strlcat(dmesgline, b, sizeof(dmesgline)); |
| 255 | } |
| 256 | } else { |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 257 | snprintf(dmesgline, sizeof(dmesgline), |
| 258 | "battery none"); |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | KLOG_INFO(LOG_TAG, "%s chg=%s%s%s\n", dmesgline, |
| 262 | props.chargerAcOnline ? "a" : "", |
| 263 | props.chargerUsbOnline ? "u" : "", |
| 264 | props.chargerWirelessOnline ? "w" : ""); |
Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 267 | healthd_mode_ops->battery_update(&props); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 268 | return props.chargerAcOnline | props.chargerUsbOnline | |
| 269 | props.chargerWirelessOnline; |
| 270 | } |
| 271 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 272 | status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) { |
| 273 | status_t ret = BAD_VALUE; |
| 274 | |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 275 | val->valueInt64 = LONG_MIN; |
| 276 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 277 | switch(id) { |
| 278 | case BATTERY_PROP_CHARGE_COUNTER: |
| 279 | if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 280 | val->valueInt64 = |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 281 | getIntField(mHealthdConfig->batteryChargeCounterPath); |
| 282 | ret = NO_ERROR; |
| 283 | } else { |
| 284 | ret = NAME_NOT_FOUND; |
| 285 | } |
| 286 | break; |
| 287 | |
| 288 | case BATTERY_PROP_CURRENT_NOW: |
| 289 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 290 | val->valueInt64 = |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 291 | getIntField(mHealthdConfig->batteryCurrentNowPath); |
| 292 | ret = NO_ERROR; |
| 293 | } else { |
| 294 | ret = NAME_NOT_FOUND; |
| 295 | } |
| 296 | break; |
| 297 | |
Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 298 | case BATTERY_PROP_CURRENT_AVG: |
| 299 | if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 300 | val->valueInt64 = |
Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 301 | getIntField(mHealthdConfig->batteryCurrentAvgPath); |
| 302 | ret = NO_ERROR; |
| 303 | } else { |
| 304 | ret = NAME_NOT_FOUND; |
| 305 | } |
| 306 | break; |
| 307 | |
Paul Lawrence | 347c8de | 2014-03-19 15:04:40 -0700 | [diff] [blame] | 308 | case BATTERY_PROP_CAPACITY: |
| 309 | if (!mHealthdConfig->batteryCapacityPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 310 | val->valueInt64 = |
Paul Lawrence | 347c8de | 2014-03-19 15:04:40 -0700 | [diff] [blame] | 311 | getIntField(mHealthdConfig->batteryCapacityPath); |
| 312 | ret = NO_ERROR; |
| 313 | } else { |
| 314 | ret = NAME_NOT_FOUND; |
| 315 | } |
| 316 | break; |
| 317 | |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 318 | case BATTERY_PROP_ENERGY_COUNTER: |
Todd Poynor | e14b37e | 2014-05-20 13:54:40 -0700 | [diff] [blame] | 319 | if (mHealthdConfig->energyCounter) { |
| 320 | ret = mHealthdConfig->energyCounter(&val->valueInt64); |
| 321 | } else { |
| 322 | ret = NAME_NOT_FOUND; |
| 323 | } |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 324 | break; |
| 325 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 326 | default: |
| 327 | break; |
| 328 | } |
| 329 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 330 | return ret; |
| 331 | } |
| 332 | |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 333 | void BatteryMonitor::dumpState(int fd) { |
| 334 | int v; |
| 335 | char vs[128]; |
| 336 | |
| 337 | snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d\n", |
| 338 | props.chargerAcOnline, props.chargerUsbOnline, |
| 339 | props.chargerWirelessOnline); |
| 340 | write(fd, vs, strlen(vs)); |
| 341 | snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n", |
| 342 | props.batteryStatus, props.batteryHealth, props.batteryPresent); |
| 343 | write(fd, vs, strlen(vs)); |
| 344 | snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n", |
| 345 | props.batteryLevel, props.batteryVoltage, |
| 346 | props.batteryTemperature); |
| 347 | write(fd, vs, strlen(vs)); |
| 348 | |
| 349 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
| 350 | v = getIntField(mHealthdConfig->batteryCurrentNowPath); |
| 351 | snprintf(vs, sizeof(vs), "current now: %d\n", v); |
| 352 | write(fd, vs, strlen(vs)); |
| 353 | } |
| 354 | |
| 355 | if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { |
| 356 | v = getIntField(mHealthdConfig->batteryCurrentAvgPath); |
| 357 | snprintf(vs, sizeof(vs), "current avg: %d\n", v); |
| 358 | write(fd, vs, strlen(vs)); |
| 359 | } |
| 360 | |
| 361 | if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) { |
| 362 | v = getIntField(mHealthdConfig->batteryChargeCounterPath); |
| 363 | snprintf(vs, sizeof(vs), "charge counter: %d\n", v); |
| 364 | write(fd, vs, strlen(vs)); |
| 365 | } |
| 366 | } |
| 367 | |
Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 368 | void BatteryMonitor::init(struct healthd_config *hc) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 369 | String8 path; |
| 370 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 371 | mHealthdConfig = hc; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 372 | DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH); |
| 373 | if (dir == NULL) { |
| 374 | KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH); |
| 375 | } else { |
| 376 | struct dirent* entry; |
| 377 | |
| 378 | while ((entry = readdir(dir))) { |
| 379 | const char* name = entry->d_name; |
| 380 | |
| 381 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
| 382 | continue; |
| 383 | |
| 384 | char buf[20]; |
| 385 | // Look for "type" file in each subdirectory |
| 386 | path.clear(); |
| 387 | path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name); |
| 388 | switch(readPowerSupplyType(path)) { |
| 389 | case ANDROID_POWER_SUPPLY_TYPE_AC: |
| 390 | case ANDROID_POWER_SUPPLY_TYPE_USB: |
| 391 | case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: |
| 392 | path.clear(); |
| 393 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); |
| 394 | if (access(path.string(), R_OK) == 0) |
| 395 | mChargerNames.add(String8(name)); |
| 396 | break; |
| 397 | |
| 398 | case ANDROID_POWER_SUPPLY_TYPE_BATTERY: |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 399 | mBatteryDevicePresent = true; |
| 400 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 401 | if (mHealthdConfig->batteryStatusPath.isEmpty()) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 402 | path.clear(); |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 403 | path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH, |
| 404 | name); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 405 | if (access(path, R_OK) == 0) |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 406 | mHealthdConfig->batteryStatusPath = path; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 409 | if (mHealthdConfig->batteryHealthPath.isEmpty()) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 410 | path.clear(); |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 411 | path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH, |
| 412 | name); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 413 | if (access(path, R_OK) == 0) |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 414 | mHealthdConfig->batteryHealthPath = path; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 417 | if (mHealthdConfig->batteryPresentPath.isEmpty()) { |
| 418 | path.clear(); |
| 419 | path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH, |
| 420 | name); |
| 421 | if (access(path, R_OK) == 0) |
| 422 | mHealthdConfig->batteryPresentPath = path; |
| 423 | } |
| 424 | |
| 425 | if (mHealthdConfig->batteryCapacityPath.isEmpty()) { |
| 426 | path.clear(); |
| 427 | path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH, |
| 428 | name); |
| 429 | if (access(path, R_OK) == 0) |
| 430 | mHealthdConfig->batteryCapacityPath = path; |
| 431 | } |
| 432 | |
| 433 | if (mHealthdConfig->batteryVoltagePath.isEmpty()) { |
| 434 | path.clear(); |
| 435 | path.appendFormat("%s/%s/voltage_now", |
| 436 | POWER_SUPPLY_SYSFS_PATH, name); |
| 437 | if (access(path, R_OK) == 0) { |
| 438 | mHealthdConfig->batteryVoltagePath = path; |
| 439 | } else { |
| 440 | path.clear(); |
| 441 | path.appendFormat("%s/%s/batt_vol", |
| 442 | POWER_SUPPLY_SYSFS_PATH, name); |
| 443 | if (access(path, R_OK) == 0) |
| 444 | mHealthdConfig->batteryVoltagePath = path; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
| 449 | path.clear(); |
| 450 | path.appendFormat("%s/%s/current_now", |
| 451 | POWER_SUPPLY_SYSFS_PATH, name); |
| 452 | if (access(path, R_OK) == 0) |
| 453 | mHealthdConfig->batteryCurrentNowPath = path; |
| 454 | } |
| 455 | |
Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 456 | if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { |
| 457 | path.clear(); |
| 458 | path.appendFormat("%s/%s/current_avg", |
| 459 | POWER_SUPPLY_SYSFS_PATH, name); |
| 460 | if (access(path, R_OK) == 0) |
| 461 | mHealthdConfig->batteryCurrentAvgPath = path; |
| 462 | } |
| 463 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 464 | if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) { |
| 465 | path.clear(); |
| 466 | path.appendFormat("%s/%s/charge_counter", |
| 467 | POWER_SUPPLY_SYSFS_PATH, name); |
| 468 | if (access(path, R_OK) == 0) |
| 469 | mHealthdConfig->batteryChargeCounterPath = path; |
| 470 | } |
| 471 | |
| 472 | if (mHealthdConfig->batteryTemperaturePath.isEmpty()) { |
| 473 | path.clear(); |
| 474 | path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH, |
| 475 | name); |
| 476 | if (access(path, R_OK) == 0) { |
| 477 | mHealthdConfig->batteryTemperaturePath = path; |
| 478 | } else { |
| 479 | path.clear(); |
| 480 | path.appendFormat("%s/%s/batt_temp", |
| 481 | POWER_SUPPLY_SYSFS_PATH, name); |
| 482 | if (access(path, R_OK) == 0) |
| 483 | mHealthdConfig->batteryTemperaturePath = path; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | if (mHealthdConfig->batteryTechnologyPath.isEmpty()) { |
| 488 | path.clear(); |
| 489 | path.appendFormat("%s/%s/technology", |
| 490 | POWER_SUPPLY_SYSFS_PATH, name); |
| 491 | if (access(path, R_OK) == 0) |
| 492 | mHealthdConfig->batteryTechnologyPath = path; |
| 493 | } |
| 494 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 495 | break; |
| 496 | |
| 497 | case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN: |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | closedir(dir); |
| 502 | } |
| 503 | |
| 504 | if (!mChargerNames.size()) |
| 505 | KLOG_ERROR(LOG_TAG, "No charger supplies found\n"); |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 506 | if (!mBatteryDevicePresent) { |
| 507 | KLOG_INFO(LOG_TAG, "No battery devices found\n"); |
| 508 | hc->periodic_chores_interval_fast = -1; |
| 509 | hc->periodic_chores_interval_slow = -1; |
| 510 | } else { |
| 511 | if (mHealthdConfig->batteryStatusPath.isEmpty()) |
| 512 | KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n"); |
| 513 | if (mHealthdConfig->batteryHealthPath.isEmpty()) |
| 514 | KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n"); |
| 515 | if (mHealthdConfig->batteryPresentPath.isEmpty()) |
| 516 | KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n"); |
| 517 | if (mHealthdConfig->batteryCapacityPath.isEmpty()) |
| 518 | KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n"); |
| 519 | if (mHealthdConfig->batteryVoltagePath.isEmpty()) |
| 520 | KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n"); |
| 521 | if (mHealthdConfig->batteryTemperaturePath.isEmpty()) |
| 522 | KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n"); |
| 523 | if (mHealthdConfig->batteryTechnologyPath.isEmpty()) |
| 524 | KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n"); |
| 525 | } |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | }; // namespace android |