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 }, |
Johan Redestig | 3282861 | 2016-02-03 13:45:54 +0100 | [diff] [blame^] | 137 | { "USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC }, |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 138 | { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC }, |
| 139 | { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC }, |
Benson Leung | 8a4eef6 | 2015-10-07 16:12:04 -0700 | [diff] [blame] | 140 | { "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC }, |
| 141 | { "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC }, |
| 142 | { "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB }, |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 143 | { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS }, |
| 144 | { NULL, 0 }, |
| 145 | }; |
| 146 | |
| 147 | if (length <= 0) |
| 148 | return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; |
| 149 | |
| 150 | ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap); |
Johan Redestig | 3282861 | 2016-02-03 13:45:54 +0100 | [diff] [blame^] | 151 | if (ret < 0) { |
| 152 | KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 153 | ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; |
Johan Redestig | 3282861 | 2016-02-03 13:45:54 +0100 | [diff] [blame^] | 154 | } |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 155 | |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | bool BatteryMonitor::getBooleanField(const String8& path) { |
| 160 | const int SIZE = 16; |
| 161 | char buf[SIZE]; |
| 162 | |
| 163 | bool value = false; |
| 164 | if (readFromFile(path, buf, SIZE) > 0) { |
| 165 | if (buf[0] != '0') { |
| 166 | value = true; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return value; |
| 171 | } |
| 172 | |
| 173 | int BatteryMonitor::getIntField(const String8& path) { |
| 174 | const int SIZE = 128; |
| 175 | char buf[SIZE]; |
| 176 | |
| 177 | int value = 0; |
| 178 | if (readFromFile(path, buf, SIZE) > 0) { |
| 179 | value = strtol(buf, NULL, 0); |
| 180 | } |
| 181 | return value; |
| 182 | } |
| 183 | |
| 184 | bool BatteryMonitor::update(void) { |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 185 | bool logthis; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 186 | |
| 187 | props.chargerAcOnline = false; |
| 188 | props.chargerUsbOnline = false; |
| 189 | props.chargerWirelessOnline = false; |
| 190 | props.batteryStatus = BATTERY_STATUS_UNKNOWN; |
| 191 | props.batteryHealth = BATTERY_HEALTH_UNKNOWN; |
Adrian Roos | d5fe667 | 2015-07-10 13:11:01 -0700 | [diff] [blame] | 192 | props.maxChargingCurrent = 0; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 193 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 194 | if (!mHealthdConfig->batteryPresentPath.isEmpty()) |
| 195 | props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 196 | else |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 197 | props.batteryPresent = mBatteryDevicePresent; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 198 | |
Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 199 | props.batteryLevel = mBatteryFixedCapacity ? |
| 200 | mBatteryFixedCapacity : |
| 201 | getIntField(mHealthdConfig->batteryCapacityPath); |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 202 | props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000; |
Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 203 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 204 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) |
| 205 | props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000; |
| 206 | |
| 207 | if (!mHealthdConfig->batteryFullChargePath.isEmpty()) |
| 208 | props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath); |
| 209 | |
| 210 | if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) |
| 211 | props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath); |
| 212 | |
Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 213 | props.batteryTemperature = mBatteryFixedTemperature ? |
| 214 | mBatteryFixedTemperature : |
| 215 | getIntField(mHealthdConfig->batteryTemperaturePath); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 216 | |
Ruchi Kandoi | 42a981d | 2015-09-28 13:35:59 -0700 | [diff] [blame] | 217 | // For devices which do not have battery and are always plugged |
| 218 | // into power souce. |
| 219 | if (mAlwaysPluggedDevice) { |
| 220 | props.chargerAcOnline = true; |
| 221 | props.batteryPresent = true; |
| 222 | props.batteryStatus = BATTERY_STATUS_CHARGING; |
| 223 | props.batteryHealth = BATTERY_HEALTH_GOOD; |
| 224 | } |
| 225 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 226 | const int SIZE = 128; |
| 227 | char buf[SIZE]; |
| 228 | String8 btech; |
| 229 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 230 | if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0) |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 231 | props.batteryStatus = getBatteryStatus(buf); |
| 232 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 233 | if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0) |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 234 | props.batteryHealth = getBatteryHealth(buf); |
| 235 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 236 | if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0) |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 237 | props.batteryTechnology = String8(buf); |
| 238 | |
| 239 | unsigned int i; |
| 240 | |
| 241 | for (i = 0; i < mChargerNames.size(); i++) { |
| 242 | String8 path; |
| 243 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, |
| 244 | mChargerNames[i].string()); |
| 245 | |
| 246 | if (readFromFile(path, buf, SIZE) > 0) { |
| 247 | if (buf[0] != '0') { |
| 248 | path.clear(); |
| 249 | path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, |
| 250 | mChargerNames[i].string()); |
| 251 | switch(readPowerSupplyType(path)) { |
| 252 | case ANDROID_POWER_SUPPLY_TYPE_AC: |
| 253 | props.chargerAcOnline = true; |
| 254 | break; |
| 255 | case ANDROID_POWER_SUPPLY_TYPE_USB: |
| 256 | props.chargerUsbOnline = true; |
| 257 | break; |
| 258 | case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: |
| 259 | props.chargerWirelessOnline = true; |
| 260 | break; |
| 261 | default: |
| 262 | KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n", |
| 263 | mChargerNames[i].string()); |
| 264 | } |
Adrian Roos | d5fe667 | 2015-07-10 13:11:01 -0700 | [diff] [blame] | 265 | path.clear(); |
| 266 | path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH, |
| 267 | mChargerNames[i].string()); |
| 268 | if (access(path.string(), R_OK) == 0) { |
| 269 | int maxChargingCurrent = getIntField(path); |
| 270 | if (props.maxChargingCurrent < maxChargingCurrent) { |
| 271 | props.maxChargingCurrent = maxChargingCurrent; |
| 272 | } |
| 273 | } |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 278 | logthis = !healthd_board_battery_update(&props); |
Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 279 | |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 280 | if (logthis) { |
| 281 | char dmesgline[256]; |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 282 | size_t len; |
Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 283 | if (props.batteryPresent) { |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 284 | snprintf(dmesgline, sizeof(dmesgline), |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 285 | "battery l=%d v=%d t=%s%d.%d h=%d st=%d", |
| 286 | props.batteryLevel, props.batteryVoltage, |
| 287 | props.batteryTemperature < 0 ? "-" : "", |
| 288 | abs(props.batteryTemperature / 10), |
| 289 | abs(props.batteryTemperature % 10), props.batteryHealth, |
| 290 | props.batteryStatus); |
Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 291 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 292 | len = strlen(dmesgline); |
Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 293 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 294 | len += snprintf(dmesgline + len, sizeof(dmesgline) - len, |
| 295 | " c=%d", props.batteryCurrent); |
| 296 | } |
Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 297 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 298 | if (!mHealthdConfig->batteryFullChargePath.isEmpty()) { |
| 299 | len += snprintf(dmesgline + len, sizeof(dmesgline) - len, |
| 300 | " fc=%d", props.batteryFullCharge); |
| 301 | } |
| 302 | |
| 303 | if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) { |
| 304 | len += snprintf(dmesgline + len, sizeof(dmesgline) - len, |
| 305 | " cc=%d", props.batteryCycleCount); |
Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 306 | } |
| 307 | } else { |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 308 | snprintf(dmesgline, sizeof(dmesgline), |
| 309 | "battery none"); |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 312 | len = strlen(dmesgline); |
Mark Salyzyn | acb1ddf | 2015-07-23 09:22:50 -0700 | [diff] [blame] | 313 | snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s", |
| 314 | props.chargerAcOnline ? "a" : "", |
| 315 | props.chargerUsbOnline ? "u" : "", |
| 316 | props.chargerWirelessOnline ? "w" : ""); |
| 317 | |
Mark Salyzyn | acb1ddf | 2015-07-23 09:22:50 -0700 | [diff] [blame] | 318 | KLOG_WARNING(LOG_TAG, "%s\n", dmesgline); |
Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 321 | healthd_mode_ops->battery_update(&props); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 322 | return props.chargerAcOnline | props.chargerUsbOnline | |
| 323 | props.chargerWirelessOnline; |
| 324 | } |
| 325 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 326 | status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) { |
| 327 | status_t ret = BAD_VALUE; |
| 328 | |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 329 | val->valueInt64 = LONG_MIN; |
| 330 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 331 | switch(id) { |
| 332 | case BATTERY_PROP_CHARGE_COUNTER: |
| 333 | if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 334 | val->valueInt64 = |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 335 | getIntField(mHealthdConfig->batteryChargeCounterPath); |
| 336 | ret = NO_ERROR; |
| 337 | } else { |
| 338 | ret = NAME_NOT_FOUND; |
| 339 | } |
| 340 | break; |
| 341 | |
| 342 | case BATTERY_PROP_CURRENT_NOW: |
| 343 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 344 | val->valueInt64 = |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 345 | getIntField(mHealthdConfig->batteryCurrentNowPath); |
| 346 | ret = NO_ERROR; |
| 347 | } else { |
| 348 | ret = NAME_NOT_FOUND; |
| 349 | } |
| 350 | break; |
| 351 | |
Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 352 | case BATTERY_PROP_CURRENT_AVG: |
| 353 | if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 354 | val->valueInt64 = |
Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 355 | getIntField(mHealthdConfig->batteryCurrentAvgPath); |
| 356 | ret = NO_ERROR; |
| 357 | } else { |
| 358 | ret = NAME_NOT_FOUND; |
| 359 | } |
| 360 | break; |
| 361 | |
Paul Lawrence | 347c8de | 2014-03-19 15:04:40 -0700 | [diff] [blame] | 362 | case BATTERY_PROP_CAPACITY: |
| 363 | if (!mHealthdConfig->batteryCapacityPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 364 | val->valueInt64 = |
Paul Lawrence | 347c8de | 2014-03-19 15:04:40 -0700 | [diff] [blame] | 365 | getIntField(mHealthdConfig->batteryCapacityPath); |
| 366 | ret = NO_ERROR; |
| 367 | } else { |
| 368 | ret = NAME_NOT_FOUND; |
| 369 | } |
| 370 | break; |
| 371 | |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 372 | case BATTERY_PROP_ENERGY_COUNTER: |
Todd Poynor | e14b37e | 2014-05-20 13:54:40 -0700 | [diff] [blame] | 373 | if (mHealthdConfig->energyCounter) { |
| 374 | ret = mHealthdConfig->energyCounter(&val->valueInt64); |
| 375 | } else { |
| 376 | ret = NAME_NOT_FOUND; |
| 377 | } |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 378 | break; |
| 379 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 380 | default: |
| 381 | break; |
| 382 | } |
| 383 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 384 | return ret; |
| 385 | } |
| 386 | |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 387 | void BatteryMonitor::dumpState(int fd) { |
| 388 | int v; |
| 389 | char vs[128]; |
| 390 | |
Adrian Roos | d5fe667 | 2015-07-10 13:11:01 -0700 | [diff] [blame] | 391 | 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] | 392 | props.chargerAcOnline, props.chargerUsbOnline, |
Adrian Roos | d5fe667 | 2015-07-10 13:11:01 -0700 | [diff] [blame] | 393 | props.chargerWirelessOnline, props.maxChargingCurrent); |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 394 | write(fd, vs, strlen(vs)); |
| 395 | snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n", |
| 396 | props.batteryStatus, props.batteryHealth, props.batteryPresent); |
| 397 | write(fd, vs, strlen(vs)); |
| 398 | snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n", |
| 399 | props.batteryLevel, props.batteryVoltage, |
| 400 | props.batteryTemperature); |
| 401 | write(fd, vs, strlen(vs)); |
| 402 | |
| 403 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
| 404 | v = getIntField(mHealthdConfig->batteryCurrentNowPath); |
| 405 | snprintf(vs, sizeof(vs), "current now: %d\n", v); |
| 406 | write(fd, vs, strlen(vs)); |
| 407 | } |
| 408 | |
| 409 | if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { |
| 410 | v = getIntField(mHealthdConfig->batteryCurrentAvgPath); |
| 411 | snprintf(vs, sizeof(vs), "current avg: %d\n", v); |
| 412 | write(fd, vs, strlen(vs)); |
| 413 | } |
| 414 | |
| 415 | if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) { |
| 416 | v = getIntField(mHealthdConfig->batteryChargeCounterPath); |
| 417 | snprintf(vs, sizeof(vs), "charge counter: %d\n", v); |
| 418 | write(fd, vs, strlen(vs)); |
| 419 | } |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 420 | |
| 421 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
| 422 | snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent); |
| 423 | write(fd, vs, strlen(vs)); |
| 424 | } |
| 425 | |
| 426 | if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) { |
| 427 | snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount); |
| 428 | write(fd, vs, strlen(vs)); |
| 429 | } |
| 430 | |
| 431 | if (!mHealthdConfig->batteryFullChargePath.isEmpty()) { |
| 432 | snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge); |
| 433 | write(fd, vs, strlen(vs)); |
| 434 | } |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 437 | void BatteryMonitor::init(struct healthd_config *hc) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 438 | String8 path; |
Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 439 | char pval[PROPERTY_VALUE_MAX]; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 440 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 441 | mHealthdConfig = hc; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 442 | DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH); |
| 443 | if (dir == NULL) { |
| 444 | KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH); |
| 445 | } else { |
| 446 | struct dirent* entry; |
| 447 | |
| 448 | while ((entry = readdir(dir))) { |
| 449 | const char* name = entry->d_name; |
| 450 | |
| 451 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
| 452 | continue; |
| 453 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 454 | // Look for "type" file in each subdirectory |
| 455 | path.clear(); |
| 456 | path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name); |
| 457 | switch(readPowerSupplyType(path)) { |
| 458 | case ANDROID_POWER_SUPPLY_TYPE_AC: |
| 459 | case ANDROID_POWER_SUPPLY_TYPE_USB: |
| 460 | case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: |
| 461 | path.clear(); |
| 462 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); |
| 463 | if (access(path.string(), R_OK) == 0) |
| 464 | mChargerNames.add(String8(name)); |
| 465 | break; |
| 466 | |
| 467 | case ANDROID_POWER_SUPPLY_TYPE_BATTERY: |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 468 | mBatteryDevicePresent = true; |
| 469 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 470 | if (mHealthdConfig->batteryStatusPath.isEmpty()) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 471 | path.clear(); |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 472 | path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH, |
| 473 | name); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 474 | if (access(path, R_OK) == 0) |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 475 | mHealthdConfig->batteryStatusPath = path; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 478 | if (mHealthdConfig->batteryHealthPath.isEmpty()) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 479 | path.clear(); |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 480 | path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH, |
| 481 | name); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 482 | if (access(path, R_OK) == 0) |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 483 | mHealthdConfig->batteryHealthPath = path; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 486 | if (mHealthdConfig->batteryPresentPath.isEmpty()) { |
| 487 | path.clear(); |
| 488 | path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH, |
| 489 | name); |
| 490 | if (access(path, R_OK) == 0) |
| 491 | mHealthdConfig->batteryPresentPath = path; |
| 492 | } |
| 493 | |
| 494 | if (mHealthdConfig->batteryCapacityPath.isEmpty()) { |
| 495 | path.clear(); |
| 496 | path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH, |
| 497 | name); |
| 498 | if (access(path, R_OK) == 0) |
| 499 | mHealthdConfig->batteryCapacityPath = path; |
| 500 | } |
| 501 | |
| 502 | if (mHealthdConfig->batteryVoltagePath.isEmpty()) { |
| 503 | path.clear(); |
| 504 | path.appendFormat("%s/%s/voltage_now", |
| 505 | POWER_SUPPLY_SYSFS_PATH, name); |
| 506 | if (access(path, R_OK) == 0) { |
| 507 | mHealthdConfig->batteryVoltagePath = path; |
| 508 | } else { |
| 509 | path.clear(); |
| 510 | path.appendFormat("%s/%s/batt_vol", |
| 511 | POWER_SUPPLY_SYSFS_PATH, name); |
| 512 | if (access(path, R_OK) == 0) |
| 513 | mHealthdConfig->batteryVoltagePath = path; |
| 514 | } |
| 515 | } |
| 516 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 517 | if (mHealthdConfig->batteryFullChargePath.isEmpty()) { |
| 518 | path.clear(); |
| 519 | path.appendFormat("%s/%s/charge_full", |
| 520 | POWER_SUPPLY_SYSFS_PATH, name); |
| 521 | if (access(path, R_OK) == 0) |
| 522 | mHealthdConfig->batteryFullChargePath = path; |
| 523 | } |
| 524 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 525 | if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
| 526 | path.clear(); |
| 527 | path.appendFormat("%s/%s/current_now", |
| 528 | POWER_SUPPLY_SYSFS_PATH, name); |
| 529 | if (access(path, R_OK) == 0) |
| 530 | mHealthdConfig->batteryCurrentNowPath = path; |
| 531 | } |
| 532 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 533 | if (mHealthdConfig->batteryCycleCountPath.isEmpty()) { |
| 534 | path.clear(); |
| 535 | path.appendFormat("%s/%s/cycle_count", |
| 536 | POWER_SUPPLY_SYSFS_PATH, name); |
| 537 | if (access(path, R_OK) == 0) |
| 538 | mHealthdConfig->batteryCycleCountPath = path; |
| 539 | } |
| 540 | |
Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 541 | if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { |
| 542 | path.clear(); |
| 543 | path.appendFormat("%s/%s/current_avg", |
| 544 | POWER_SUPPLY_SYSFS_PATH, name); |
| 545 | if (access(path, R_OK) == 0) |
| 546 | mHealthdConfig->batteryCurrentAvgPath = path; |
| 547 | } |
| 548 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 549 | if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) { |
| 550 | path.clear(); |
| 551 | path.appendFormat("%s/%s/charge_counter", |
| 552 | POWER_SUPPLY_SYSFS_PATH, name); |
| 553 | if (access(path, R_OK) == 0) |
| 554 | mHealthdConfig->batteryChargeCounterPath = path; |
| 555 | } |
| 556 | |
| 557 | if (mHealthdConfig->batteryTemperaturePath.isEmpty()) { |
| 558 | path.clear(); |
| 559 | path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH, |
| 560 | name); |
| 561 | if (access(path, R_OK) == 0) { |
| 562 | mHealthdConfig->batteryTemperaturePath = path; |
| 563 | } else { |
| 564 | path.clear(); |
| 565 | path.appendFormat("%s/%s/batt_temp", |
| 566 | POWER_SUPPLY_SYSFS_PATH, name); |
| 567 | if (access(path, R_OK) == 0) |
| 568 | mHealthdConfig->batteryTemperaturePath = path; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | if (mHealthdConfig->batteryTechnologyPath.isEmpty()) { |
| 573 | path.clear(); |
| 574 | path.appendFormat("%s/%s/technology", |
| 575 | POWER_SUPPLY_SYSFS_PATH, name); |
| 576 | if (access(path, R_OK) == 0) |
| 577 | mHealthdConfig->batteryTechnologyPath = path; |
| 578 | } |
| 579 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 580 | break; |
| 581 | |
| 582 | case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN: |
| 583 | break; |
| 584 | } |
| 585 | } |
| 586 | closedir(dir); |
| 587 | } |
| 588 | |
Ruchi Kandoi | 42a981d | 2015-09-28 13:35:59 -0700 | [diff] [blame] | 589 | // This indicates that there is no charger driver registered. |
| 590 | // Typically the case for devices which do not have a battery and |
| 591 | // and are always plugged into AC mains. |
| 592 | if (!mChargerNames.size()) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 593 | KLOG_ERROR(LOG_TAG, "No charger supplies found\n"); |
Ruchi Kandoi | 42a981d | 2015-09-28 13:35:59 -0700 | [diff] [blame] | 594 | mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY; |
| 595 | mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE; |
| 596 | mAlwaysPluggedDevice = true; |
| 597 | } |
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 |