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> |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 33 | #include <utils/Errors.h> |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 34 | #include <utils/String8.h> |
| 35 | #include <utils/Vector.h> |
| 36 | |
| 37 | #define POWER_SUPPLY_SUBSYSTEM "power_supply" |
| 38 | #define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM |
Ruchi Kandoi | a78fc23 | 2014-07-10 15:06:21 -0700 | [diff] [blame] | 39 | #define FAKE_BATTERY_CAPACITY 42 |
| 40 | #define FAKE_BATTERY_TEMPERATURE 424 |
Ruchi Kandoi | 42a981d | 2015-09-28 13:35:59 -0700 | [diff] [blame] | 41 | #define ALWAYS_PLUGGED_CAPACITY 100 |
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 | |
Ruchi Kandoi | 42a981d | 2015-09-28 13:35:59 -0700 | [diff] [blame] | 214 | // For devices which do not have battery and are always plugged |
| 215 | // into power souce. |
| 216 | if (mAlwaysPluggedDevice) { |
| 217 | props.chargerAcOnline = true; |
| 218 | props.batteryPresent = true; |
| 219 | props.batteryStatus = BATTERY_STATUS_CHARGING; |
| 220 | props.batteryHealth = BATTERY_HEALTH_GOOD; |
| 221 | } |
| 222 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 223 | const int SIZE = 128; |
| 224 | char buf[SIZE]; |
| 225 | String8 btech; |
| 226 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 227 | if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0) |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 228 | props.batteryStatus = getBatteryStatus(buf); |
| 229 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 230 | if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0) |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 231 | props.batteryHealth = getBatteryHealth(buf); |
| 232 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 233 | if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0) |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 234 | props.batteryTechnology = String8(buf); |
| 235 | |
| 236 | unsigned int i; |
| 237 | |
| 238 | for (i = 0; i < mChargerNames.size(); i++) { |
| 239 | String8 path; |
| 240 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, |
| 241 | mChargerNames[i].string()); |
| 242 | |
| 243 | if (readFromFile(path, buf, SIZE) > 0) { |
| 244 | if (buf[0] != '0') { |
| 245 | path.clear(); |
| 246 | path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, |
| 247 | mChargerNames[i].string()); |
| 248 | switch(readPowerSupplyType(path)) { |
| 249 | case ANDROID_POWER_SUPPLY_TYPE_AC: |
| 250 | props.chargerAcOnline = true; |
| 251 | break; |
| 252 | case ANDROID_POWER_SUPPLY_TYPE_USB: |
| 253 | props.chargerUsbOnline = true; |
| 254 | break; |
| 255 | case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: |
| 256 | props.chargerWirelessOnline = true; |
| 257 | break; |
| 258 | default: |
| 259 | KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n", |
| 260 | mChargerNames[i].string()); |
| 261 | } |
Adrian Roos | d5fe667 | 2015-07-10 13:11:01 -0700 | [diff] [blame] | 262 | path.clear(); |
| 263 | path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH, |
| 264 | mChargerNames[i].string()); |
| 265 | if (access(path.string(), R_OK) == 0) { |
| 266 | int maxChargingCurrent = getIntField(path); |
| 267 | if (props.maxChargingCurrent < maxChargingCurrent) { |
| 268 | props.maxChargingCurrent = maxChargingCurrent; |
| 269 | } |
| 270 | } |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 275 | logthis = !healthd_board_battery_update(&props); |
Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 276 | |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 277 | if (logthis) { |
| 278 | char dmesgline[256]; |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 279 | size_t len; |
Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 280 | if (props.batteryPresent) { |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 281 | snprintf(dmesgline, sizeof(dmesgline), |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 282 | "battery l=%d v=%d t=%s%d.%d h=%d st=%d", |
| 283 | props.batteryLevel, props.batteryVoltage, |
| 284 | props.batteryTemperature < 0 ? "-" : "", |
| 285 | abs(props.batteryTemperature / 10), |
| 286 | abs(props.batteryTemperature % 10), props.batteryHealth, |
| 287 | props.batteryStatus); |
Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 288 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 289 | len = strlen(dmesgline); |
Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 290 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 291 | len += snprintf(dmesgline + len, sizeof(dmesgline) - len, |
| 292 | " c=%d", props.batteryCurrent); |
| 293 | } |
Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 294 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 295 | if (!mHealthdConfig->batteryFullChargePath.isEmpty()) { |
| 296 | len += snprintf(dmesgline + len, sizeof(dmesgline) - len, |
| 297 | " fc=%d", props.batteryFullCharge); |
| 298 | } |
| 299 | |
| 300 | if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) { |
| 301 | len += snprintf(dmesgline + len, sizeof(dmesgline) - len, |
| 302 | " cc=%d", props.batteryCycleCount); |
Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 303 | } |
| 304 | } else { |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 305 | snprintf(dmesgline, sizeof(dmesgline), |
| 306 | "battery none"); |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 309 | len = strlen(dmesgline); |
Mark Salyzyn | acb1ddf | 2015-07-23 09:22:50 -0700 | [diff] [blame] | 310 | snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s", |
| 311 | props.chargerAcOnline ? "a" : "", |
| 312 | props.chargerUsbOnline ? "u" : "", |
| 313 | props.chargerWirelessOnline ? "w" : ""); |
| 314 | |
Mark Salyzyn | acb1ddf | 2015-07-23 09:22:50 -0700 | [diff] [blame] | 315 | KLOG_WARNING(LOG_TAG, "%s\n", dmesgline); |
Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 318 | healthd_mode_ops->battery_update(&props); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 319 | return props.chargerAcOnline | props.chargerUsbOnline | |
| 320 | props.chargerWirelessOnline; |
| 321 | } |
| 322 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 323 | status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) { |
| 324 | status_t ret = BAD_VALUE; |
| 325 | |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 326 | val->valueInt64 = LONG_MIN; |
| 327 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 328 | switch(id) { |
| 329 | case BATTERY_PROP_CHARGE_COUNTER: |
| 330 | if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 331 | val->valueInt64 = |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 332 | getIntField(mHealthdConfig->batteryChargeCounterPath); |
| 333 | ret = NO_ERROR; |
| 334 | } else { |
| 335 | ret = NAME_NOT_FOUND; |
| 336 | } |
| 337 | break; |
| 338 | |
| 339 | case BATTERY_PROP_CURRENT_NOW: |
| 340 | if (!mHealthdConfig->batteryCurrentNowPath.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->batteryCurrentNowPath); |
| 343 | ret = NO_ERROR; |
| 344 | } else { |
| 345 | ret = NAME_NOT_FOUND; |
| 346 | } |
| 347 | break; |
| 348 | |
Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 349 | case BATTERY_PROP_CURRENT_AVG: |
| 350 | if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 351 | val->valueInt64 = |
Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 352 | getIntField(mHealthdConfig->batteryCurrentAvgPath); |
| 353 | ret = NO_ERROR; |
| 354 | } else { |
| 355 | ret = NAME_NOT_FOUND; |
| 356 | } |
| 357 | break; |
| 358 | |
Paul Lawrence | 347c8de | 2014-03-19 15:04:40 -0700 | [diff] [blame] | 359 | case BATTERY_PROP_CAPACITY: |
| 360 | if (!mHealthdConfig->batteryCapacityPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 361 | val->valueInt64 = |
Paul Lawrence | 347c8de | 2014-03-19 15:04:40 -0700 | [diff] [blame] | 362 | getIntField(mHealthdConfig->batteryCapacityPath); |
| 363 | ret = NO_ERROR; |
| 364 | } else { |
| 365 | ret = NAME_NOT_FOUND; |
| 366 | } |
| 367 | break; |
| 368 | |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 369 | case BATTERY_PROP_ENERGY_COUNTER: |
Todd Poynor | e14b37e | 2014-05-20 13:54:40 -0700 | [diff] [blame] | 370 | if (mHealthdConfig->energyCounter) { |
| 371 | ret = mHealthdConfig->energyCounter(&val->valueInt64); |
| 372 | } else { |
| 373 | ret = NAME_NOT_FOUND; |
| 374 | } |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 375 | break; |
| 376 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 377 | default: |
| 378 | break; |
| 379 | } |
| 380 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 381 | return ret; |
| 382 | } |
| 383 | |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 384 | void BatteryMonitor::dumpState(int fd) { |
| 385 | int v; |
| 386 | char vs[128]; |
| 387 | |
Adrian Roos | d5fe667 | 2015-07-10 13:11:01 -0700 | [diff] [blame] | 388 | 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] | 389 | props.chargerAcOnline, props.chargerUsbOnline, |
Adrian Roos | d5fe667 | 2015-07-10 13:11:01 -0700 | [diff] [blame] | 390 | props.chargerWirelessOnline, props.maxChargingCurrent); |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 391 | write(fd, vs, strlen(vs)); |
| 392 | snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n", |
| 393 | props.batteryStatus, props.batteryHealth, props.batteryPresent); |
| 394 | write(fd, vs, strlen(vs)); |
| 395 | snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n", |
| 396 | props.batteryLevel, props.batteryVoltage, |
| 397 | props.batteryTemperature); |
| 398 | write(fd, vs, strlen(vs)); |
| 399 | |
| 400 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
| 401 | v = getIntField(mHealthdConfig->batteryCurrentNowPath); |
| 402 | snprintf(vs, sizeof(vs), "current now: %d\n", v); |
| 403 | write(fd, vs, strlen(vs)); |
| 404 | } |
| 405 | |
| 406 | if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { |
| 407 | v = getIntField(mHealthdConfig->batteryCurrentAvgPath); |
| 408 | snprintf(vs, sizeof(vs), "current avg: %d\n", v); |
| 409 | write(fd, vs, strlen(vs)); |
| 410 | } |
| 411 | |
| 412 | if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) { |
| 413 | v = getIntField(mHealthdConfig->batteryChargeCounterPath); |
| 414 | snprintf(vs, sizeof(vs), "charge counter: %d\n", v); |
| 415 | write(fd, vs, strlen(vs)); |
| 416 | } |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 417 | |
| 418 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
| 419 | snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent); |
| 420 | write(fd, vs, strlen(vs)); |
| 421 | } |
| 422 | |
| 423 | if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) { |
| 424 | snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount); |
| 425 | write(fd, vs, strlen(vs)); |
| 426 | } |
| 427 | |
| 428 | if (!mHealthdConfig->batteryFullChargePath.isEmpty()) { |
| 429 | snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge); |
| 430 | write(fd, vs, strlen(vs)); |
| 431 | } |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 434 | void BatteryMonitor::init(struct healthd_config *hc) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 435 | String8 path; |
Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 436 | char pval[PROPERTY_VALUE_MAX]; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 437 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 438 | mHealthdConfig = hc; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 439 | DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH); |
| 440 | if (dir == NULL) { |
| 441 | KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH); |
| 442 | } else { |
| 443 | struct dirent* entry; |
| 444 | |
| 445 | while ((entry = readdir(dir))) { |
| 446 | const char* name = entry->d_name; |
| 447 | |
| 448 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
| 449 | continue; |
| 450 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 451 | // Look for "type" file in each subdirectory |
| 452 | path.clear(); |
| 453 | path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name); |
| 454 | switch(readPowerSupplyType(path)) { |
| 455 | case ANDROID_POWER_SUPPLY_TYPE_AC: |
| 456 | case ANDROID_POWER_SUPPLY_TYPE_USB: |
| 457 | case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: |
| 458 | path.clear(); |
| 459 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); |
| 460 | if (access(path.string(), R_OK) == 0) |
| 461 | mChargerNames.add(String8(name)); |
| 462 | break; |
| 463 | |
| 464 | case ANDROID_POWER_SUPPLY_TYPE_BATTERY: |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 465 | mBatteryDevicePresent = true; |
| 466 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 467 | if (mHealthdConfig->batteryStatusPath.isEmpty()) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 468 | path.clear(); |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 469 | path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH, |
| 470 | name); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 471 | if (access(path, R_OK) == 0) |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 472 | mHealthdConfig->batteryStatusPath = path; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 475 | if (mHealthdConfig->batteryHealthPath.isEmpty()) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 476 | path.clear(); |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 477 | path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH, |
| 478 | name); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 479 | if (access(path, R_OK) == 0) |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 480 | mHealthdConfig->batteryHealthPath = path; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 483 | if (mHealthdConfig->batteryPresentPath.isEmpty()) { |
| 484 | path.clear(); |
| 485 | path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH, |
| 486 | name); |
| 487 | if (access(path, R_OK) == 0) |
| 488 | mHealthdConfig->batteryPresentPath = path; |
| 489 | } |
| 490 | |
| 491 | if (mHealthdConfig->batteryCapacityPath.isEmpty()) { |
| 492 | path.clear(); |
| 493 | path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH, |
| 494 | name); |
| 495 | if (access(path, R_OK) == 0) |
| 496 | mHealthdConfig->batteryCapacityPath = path; |
| 497 | } |
| 498 | |
| 499 | if (mHealthdConfig->batteryVoltagePath.isEmpty()) { |
| 500 | path.clear(); |
| 501 | path.appendFormat("%s/%s/voltage_now", |
| 502 | POWER_SUPPLY_SYSFS_PATH, name); |
| 503 | if (access(path, R_OK) == 0) { |
| 504 | mHealthdConfig->batteryVoltagePath = path; |
| 505 | } else { |
| 506 | path.clear(); |
| 507 | path.appendFormat("%s/%s/batt_vol", |
| 508 | POWER_SUPPLY_SYSFS_PATH, name); |
| 509 | if (access(path, R_OK) == 0) |
| 510 | mHealthdConfig->batteryVoltagePath = path; |
| 511 | } |
| 512 | } |
| 513 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 514 | if (mHealthdConfig->batteryFullChargePath.isEmpty()) { |
| 515 | path.clear(); |
| 516 | path.appendFormat("%s/%s/charge_full", |
| 517 | POWER_SUPPLY_SYSFS_PATH, name); |
| 518 | if (access(path, R_OK) == 0) |
| 519 | mHealthdConfig->batteryFullChargePath = path; |
| 520 | } |
| 521 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 522 | if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
| 523 | path.clear(); |
| 524 | path.appendFormat("%s/%s/current_now", |
| 525 | POWER_SUPPLY_SYSFS_PATH, name); |
| 526 | if (access(path, R_OK) == 0) |
| 527 | mHealthdConfig->batteryCurrentNowPath = path; |
| 528 | } |
| 529 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 530 | if (mHealthdConfig->batteryCycleCountPath.isEmpty()) { |
| 531 | path.clear(); |
| 532 | path.appendFormat("%s/%s/cycle_count", |
| 533 | POWER_SUPPLY_SYSFS_PATH, name); |
| 534 | if (access(path, R_OK) == 0) |
| 535 | mHealthdConfig->batteryCycleCountPath = path; |
| 536 | } |
| 537 | |
Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 538 | if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { |
| 539 | path.clear(); |
| 540 | path.appendFormat("%s/%s/current_avg", |
| 541 | POWER_SUPPLY_SYSFS_PATH, name); |
| 542 | if (access(path, R_OK) == 0) |
| 543 | mHealthdConfig->batteryCurrentAvgPath = path; |
| 544 | } |
| 545 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 546 | if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) { |
| 547 | path.clear(); |
| 548 | path.appendFormat("%s/%s/charge_counter", |
| 549 | POWER_SUPPLY_SYSFS_PATH, name); |
| 550 | if (access(path, R_OK) == 0) |
| 551 | mHealthdConfig->batteryChargeCounterPath = path; |
| 552 | } |
| 553 | |
| 554 | if (mHealthdConfig->batteryTemperaturePath.isEmpty()) { |
| 555 | path.clear(); |
| 556 | path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH, |
| 557 | name); |
| 558 | if (access(path, R_OK) == 0) { |
| 559 | mHealthdConfig->batteryTemperaturePath = path; |
| 560 | } else { |
| 561 | path.clear(); |
| 562 | path.appendFormat("%s/%s/batt_temp", |
| 563 | POWER_SUPPLY_SYSFS_PATH, name); |
| 564 | if (access(path, R_OK) == 0) |
| 565 | mHealthdConfig->batteryTemperaturePath = path; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | if (mHealthdConfig->batteryTechnologyPath.isEmpty()) { |
| 570 | path.clear(); |
| 571 | path.appendFormat("%s/%s/technology", |
| 572 | POWER_SUPPLY_SYSFS_PATH, name); |
| 573 | if (access(path, R_OK) == 0) |
| 574 | mHealthdConfig->batteryTechnologyPath = path; |
| 575 | } |
| 576 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 577 | break; |
| 578 | |
| 579 | case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN: |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | closedir(dir); |
| 584 | } |
| 585 | |
Ruchi Kandoi | 42a981d | 2015-09-28 13:35:59 -0700 | [diff] [blame] | 586 | // This indicates that there is no charger driver registered. |
| 587 | // Typically the case for devices which do not have a battery and |
| 588 | // and are always plugged into AC mains. |
| 589 | if (!mChargerNames.size()) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 590 | KLOG_ERROR(LOG_TAG, "No charger supplies found\n"); |
Ruchi Kandoi | 42a981d | 2015-09-28 13:35:59 -0700 | [diff] [blame] | 591 | mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY; |
| 592 | mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE; |
| 593 | mAlwaysPluggedDevice = true; |
| 594 | } |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 595 | if (!mBatteryDevicePresent) { |
Todd Poynor | ebeb0c0 | 2014-09-23 14:54:24 -0700 | [diff] [blame] | 596 | KLOG_WARNING(LOG_TAG, "No battery devices found\n"); |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 597 | hc->periodic_chores_interval_fast = -1; |
| 598 | hc->periodic_chores_interval_slow = -1; |
| 599 | } else { |
| 600 | if (mHealthdConfig->batteryStatusPath.isEmpty()) |
| 601 | KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n"); |
| 602 | if (mHealthdConfig->batteryHealthPath.isEmpty()) |
| 603 | KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n"); |
| 604 | if (mHealthdConfig->batteryPresentPath.isEmpty()) |
| 605 | KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n"); |
| 606 | if (mHealthdConfig->batteryCapacityPath.isEmpty()) |
| 607 | KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n"); |
| 608 | if (mHealthdConfig->batteryVoltagePath.isEmpty()) |
| 609 | KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n"); |
| 610 | if (mHealthdConfig->batteryTemperaturePath.isEmpty()) |
| 611 | KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n"); |
| 612 | if (mHealthdConfig->batteryTechnologyPath.isEmpty()) |
| 613 | KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n"); |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 614 | if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) |
| 615 | KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n"); |
| 616 | if (mHealthdConfig->batteryFullChargePath.isEmpty()) |
| 617 | KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n"); |
| 618 | if (mHealthdConfig->batteryCycleCountPath.isEmpty()) |
| 619 | KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n"); |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 620 | } |
Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 621 | |
Ruchi Kandoi | a78fc23 | 2014-07-10 15:06:21 -0700 | [diff] [blame] | 622 | if (property_get("ro.boot.fake_battery", pval, NULL) > 0 |
| 623 | && strtol(pval, NULL, 10) != 0) { |
| 624 | mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY; |
| 625 | mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE; |
| 626 | } |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | }; // namespace android |