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