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