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