| 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 | bc193c5 | 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 | bc193c5 | 2015-07-23 09:22:50 -0700 | [diff] [blame] | 29 |  | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 30 | #include <batteryservice/BatteryService.h> | 
|  | 31 | #include <cutils/klog.h> | 
| Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 32 | #include <cutils/properties.h> | 
| Mark Salyzyn | bc193c5 | 2015-07-23 09:22:50 -0700 | [diff] [blame] | 33 | #include <log/log_read.h> | 
| Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 34 | #include <utils/Errors.h> | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 35 | #include <utils/String8.h> | 
|  | 36 | #include <utils/Vector.h> | 
|  | 37 |  | 
|  | 38 | #define POWER_SUPPLY_SUBSYSTEM "power_supply" | 
|  | 39 | #define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM | 
| Ruchi Kandoi | a78fc23 | 2014-07-10 15:06:21 -0700 | [diff] [blame] | 40 | #define FAKE_BATTERY_CAPACITY 42 | 
|  | 41 | #define FAKE_BATTERY_TEMPERATURE 424 | 
| Ruchi Kandoi | 42a981d | 2015-09-28 13:35:59 -0700 | [diff] [blame] | 42 | #define ALWAYS_PLUGGED_CAPACITY 100 | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 43 |  | 
|  | 44 | namespace android { | 
|  | 45 |  | 
|  | 46 | struct sysfsStringEnumMap { | 
| Mark Salyzyn | 6f5b47f | 2014-05-15 15:00:59 -0700 | [diff] [blame] | 47 | const char* s; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 48 | int val; | 
|  | 49 | }; | 
|  | 50 |  | 
|  | 51 | static int mapSysfsString(const char* str, | 
|  | 52 | struct sysfsStringEnumMap map[]) { | 
|  | 53 | for (int i = 0; map[i].s; i++) | 
|  | 54 | if (!strcmp(str, map[i].s)) | 
|  | 55 | return map[i].val; | 
|  | 56 |  | 
|  | 57 | return -1; | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | int BatteryMonitor::getBatteryStatus(const char* status) { | 
|  | 61 | int ret; | 
|  | 62 | struct sysfsStringEnumMap batteryStatusMap[] = { | 
|  | 63 | { "Unknown", BATTERY_STATUS_UNKNOWN }, | 
|  | 64 | { "Charging", BATTERY_STATUS_CHARGING }, | 
|  | 65 | { "Discharging", BATTERY_STATUS_DISCHARGING }, | 
|  | 66 | { "Not charging", BATTERY_STATUS_NOT_CHARGING }, | 
|  | 67 | { "Full", BATTERY_STATUS_FULL }, | 
|  | 68 | { NULL, 0 }, | 
|  | 69 | }; | 
|  | 70 |  | 
|  | 71 | ret = mapSysfsString(status, batteryStatusMap); | 
|  | 72 | if (ret < 0) { | 
|  | 73 | KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status); | 
|  | 74 | ret = BATTERY_STATUS_UNKNOWN; | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | return ret; | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | int BatteryMonitor::getBatteryHealth(const char* status) { | 
|  | 81 | int ret; | 
|  | 82 | struct sysfsStringEnumMap batteryHealthMap[] = { | 
|  | 83 | { "Unknown", BATTERY_HEALTH_UNKNOWN }, | 
|  | 84 | { "Good", BATTERY_HEALTH_GOOD }, | 
|  | 85 | { "Overheat", BATTERY_HEALTH_OVERHEAT }, | 
|  | 86 | { "Dead", BATTERY_HEALTH_DEAD }, | 
|  | 87 | { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE }, | 
|  | 88 | { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE }, | 
|  | 89 | { "Cold", BATTERY_HEALTH_COLD }, | 
|  | 90 | { NULL, 0 }, | 
|  | 91 | }; | 
|  | 92 |  | 
|  | 93 | ret = mapSysfsString(status, batteryHealthMap); | 
|  | 94 | if (ret < 0) { | 
|  | 95 | KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status); | 
|  | 96 | ret = BATTERY_HEALTH_UNKNOWN; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | return ret; | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | int BatteryMonitor::readFromFile(const String8& path, char* buf, size_t size) { | 
|  | 103 | char *cp = NULL; | 
|  | 104 |  | 
|  | 105 | if (path.isEmpty()) | 
|  | 106 | return -1; | 
|  | 107 | int fd = open(path.string(), O_RDONLY, 0); | 
|  | 108 | if (fd == -1) { | 
|  | 109 | KLOG_ERROR(LOG_TAG, "Could not open '%s'\n", path.string()); | 
|  | 110 | return -1; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | ssize_t count = TEMP_FAILURE_RETRY(read(fd, buf, size)); | 
|  | 114 | if (count > 0) | 
|  | 115 | cp = (char *)memrchr(buf, '\n', count); | 
|  | 116 |  | 
|  | 117 | if (cp) | 
|  | 118 | *cp = '\0'; | 
|  | 119 | else | 
|  | 120 | buf[0] = '\0'; | 
|  | 121 |  | 
|  | 122 | close(fd); | 
|  | 123 | return count; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) { | 
|  | 127 | const int SIZE = 128; | 
|  | 128 | char buf[SIZE]; | 
|  | 129 | int length = readFromFile(path, buf, SIZE); | 
|  | 130 | BatteryMonitor::PowerSupplyType ret; | 
|  | 131 | struct sysfsStringEnumMap supplyTypeMap[] = { | 
|  | 132 | { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN }, | 
|  | 133 | { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY }, | 
|  | 134 | { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC }, | 
|  | 135 | { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC }, | 
|  | 136 | { "USB", ANDROID_POWER_SUPPLY_TYPE_USB }, | 
|  | 137 | { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC }, | 
|  | 138 | { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC }, | 
|  | 139 | { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC }, | 
|  | 140 | { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS }, | 
|  | 141 | { NULL, 0 }, | 
|  | 142 | }; | 
|  | 143 |  | 
|  | 144 | if (length <= 0) | 
|  | 145 | return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; | 
|  | 146 |  | 
|  | 147 | ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap); | 
|  | 148 | if (ret < 0) | 
|  | 149 | ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; | 
|  | 150 |  | 
|  | 151 | return ret; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | bool BatteryMonitor::getBooleanField(const String8& path) { | 
|  | 155 | const int SIZE = 16; | 
|  | 156 | char buf[SIZE]; | 
|  | 157 |  | 
|  | 158 | bool value = false; | 
|  | 159 | if (readFromFile(path, buf, SIZE) > 0) { | 
|  | 160 | if (buf[0] != '0') { | 
|  | 161 | value = true; | 
|  | 162 | } | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | return value; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | int BatteryMonitor::getIntField(const String8& path) { | 
|  | 169 | const int SIZE = 128; | 
|  | 170 | char buf[SIZE]; | 
|  | 171 |  | 
|  | 172 | int value = 0; | 
|  | 173 | if (readFromFile(path, buf, SIZE) > 0) { | 
|  | 174 | value = strtol(buf, NULL, 0); | 
|  | 175 | } | 
|  | 176 | return value; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | bool BatteryMonitor::update(void) { | 
| Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 180 | bool logthis; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 181 |  | 
|  | 182 | props.chargerAcOnline = false; | 
|  | 183 | props.chargerUsbOnline = false; | 
|  | 184 | props.chargerWirelessOnline = false; | 
|  | 185 | props.batteryStatus = BATTERY_STATUS_UNKNOWN; | 
|  | 186 | props.batteryHealth = BATTERY_HEALTH_UNKNOWN; | 
| Adrian Roos | d5fe667 | 2015-07-10 13:11:01 -0700 | [diff] [blame] | 187 | props.maxChargingCurrent = 0; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 188 |  | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 189 | if (!mHealthdConfig->batteryPresentPath.isEmpty()) | 
|  | 190 | props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath); | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 191 | else | 
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 192 | props.batteryPresent = mBatteryDevicePresent; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 193 |  | 
| Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 194 | props.batteryLevel = mBatteryFixedCapacity ? | 
|  | 195 | mBatteryFixedCapacity : | 
|  | 196 | getIntField(mHealthdConfig->batteryCapacityPath); | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 197 | props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000; | 
| Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 198 |  | 
| Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 199 | props.batteryTemperature = mBatteryFixedTemperature ? | 
|  | 200 | mBatteryFixedTemperature : | 
|  | 201 | getIntField(mHealthdConfig->batteryTemperaturePath); | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 202 |  | 
| Ruchi Kandoi | 42a981d | 2015-09-28 13:35:59 -0700 | [diff] [blame] | 203 | // For devices which do not have battery and are always plugged | 
|  | 204 | // into power souce. | 
|  | 205 | if (mAlwaysPluggedDevice) { | 
|  | 206 | props.chargerAcOnline = true; | 
|  | 207 | props.batteryPresent = true; | 
|  | 208 | props.batteryStatus = BATTERY_STATUS_CHARGING; | 
|  | 209 | props.batteryHealth = BATTERY_HEALTH_GOOD; | 
|  | 210 | } | 
|  | 211 |  | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 212 | const int SIZE = 128; | 
|  | 213 | char buf[SIZE]; | 
|  | 214 | String8 btech; | 
|  | 215 |  | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 216 | if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0) | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 217 | props.batteryStatus = getBatteryStatus(buf); | 
|  | 218 |  | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 219 | if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0) | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 220 | props.batteryHealth = getBatteryHealth(buf); | 
|  | 221 |  | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 222 | if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0) | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 223 | props.batteryTechnology = String8(buf); | 
|  | 224 |  | 
|  | 225 | unsigned int i; | 
|  | 226 |  | 
|  | 227 | for (i = 0; i < mChargerNames.size(); i++) { | 
|  | 228 | String8 path; | 
|  | 229 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, | 
|  | 230 | mChargerNames[i].string()); | 
|  | 231 |  | 
|  | 232 | if (readFromFile(path, buf, SIZE) > 0) { | 
|  | 233 | if (buf[0] != '0') { | 
|  | 234 | path.clear(); | 
|  | 235 | path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, | 
|  | 236 | mChargerNames[i].string()); | 
|  | 237 | switch(readPowerSupplyType(path)) { | 
|  | 238 | case ANDROID_POWER_SUPPLY_TYPE_AC: | 
|  | 239 | props.chargerAcOnline = true; | 
|  | 240 | break; | 
|  | 241 | case ANDROID_POWER_SUPPLY_TYPE_USB: | 
|  | 242 | props.chargerUsbOnline = true; | 
|  | 243 | break; | 
|  | 244 | case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: | 
|  | 245 | props.chargerWirelessOnline = true; | 
|  | 246 | break; | 
|  | 247 | default: | 
|  | 248 | KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n", | 
|  | 249 | mChargerNames[i].string()); | 
|  | 250 | } | 
| Adrian Roos | d5fe667 | 2015-07-10 13:11:01 -0700 | [diff] [blame] | 251 | path.clear(); | 
|  | 252 | path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH, | 
|  | 253 | mChargerNames[i].string()); | 
|  | 254 | if (access(path.string(), R_OK) == 0) { | 
|  | 255 | int maxChargingCurrent = getIntField(path); | 
|  | 256 | if (props.maxChargingCurrent < maxChargingCurrent) { | 
|  | 257 | props.maxChargingCurrent = maxChargingCurrent; | 
|  | 258 | } | 
|  | 259 | } | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 260 | } | 
|  | 261 | } | 
|  | 262 | } | 
|  | 263 |  | 
| Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 264 | logthis = !healthd_board_battery_update(&props); | 
| Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 265 |  | 
| Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 266 | if (logthis) { | 
|  | 267 | char dmesgline[256]; | 
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 268 |  | 
| Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 269 | if (props.batteryPresent) { | 
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 270 | snprintf(dmesgline, sizeof(dmesgline), | 
| Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 271 | "battery l=%d v=%d t=%s%d.%d h=%d st=%d", | 
|  | 272 | props.batteryLevel, props.batteryVoltage, | 
|  | 273 | props.batteryTemperature < 0 ? "-" : "", | 
|  | 274 | abs(props.batteryTemperature / 10), | 
|  | 275 | abs(props.batteryTemperature % 10), props.batteryHealth, | 
|  | 276 | props.batteryStatus); | 
| Todd Poynor | c15e993 | 2013-10-22 18:21:27 -0700 | [diff] [blame] | 277 |  | 
|  | 278 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { | 
|  | 279 | int c = getIntField(mHealthdConfig->batteryCurrentNowPath); | 
|  | 280 | char b[20]; | 
|  | 281 |  | 
|  | 282 | snprintf(b, sizeof(b), " c=%d", c / 1000); | 
|  | 283 | strlcat(dmesgline, b, sizeof(dmesgline)); | 
|  | 284 | } | 
|  | 285 | } else { | 
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 286 | snprintf(dmesgline, sizeof(dmesgline), | 
|  | 287 | "battery none"); | 
| Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 288 | } | 
|  | 289 |  | 
| Mark Salyzyn | bc193c5 | 2015-07-23 09:22:50 -0700 | [diff] [blame] | 290 | size_t len = strlen(dmesgline); | 
|  | 291 | snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s", | 
|  | 292 | props.chargerAcOnline ? "a" : "", | 
|  | 293 | props.chargerUsbOnline ? "u" : "", | 
|  | 294 | props.chargerWirelessOnline ? "w" : ""); | 
|  | 295 |  | 
|  | 296 | log_time realtime(CLOCK_REALTIME); | 
|  | 297 | time_t t = realtime.tv_sec; | 
|  | 298 | struct tm *tmp = gmtime(&t); | 
|  | 299 | if (tmp) { | 
|  | 300 | static const char fmt[] = " %Y-%m-%d %H:%M:%S.XXXXXXXXX UTC"; | 
|  | 301 | len = strlen(dmesgline); | 
|  | 302 | if ((len < (sizeof(dmesgline) - sizeof(fmt) - 8)) // margin | 
|  | 303 | && strftime(dmesgline + len, sizeof(dmesgline) - len, | 
|  | 304 | fmt, tmp)) { | 
|  | 305 | char *usec = strchr(dmesgline + len, 'X'); | 
|  | 306 | if (usec) { | 
|  | 307 | len = usec - dmesgline; | 
|  | 308 | snprintf(dmesgline + len, sizeof(dmesgline) - len, | 
|  | 309 | "%09u", realtime.tv_nsec); | 
|  | 310 | usec[9] = ' '; | 
|  | 311 | } | 
|  | 312 | } | 
|  | 313 | } | 
|  | 314 |  | 
|  | 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 | } | 
|  | 417 | } | 
|  | 418 |  | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 419 | void BatteryMonitor::init(struct healthd_config *hc) { | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 420 | String8 path; | 
| Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 421 | char pval[PROPERTY_VALUE_MAX]; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 422 |  | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 423 | mHealthdConfig = hc; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 424 | DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH); | 
|  | 425 | if (dir == NULL) { | 
|  | 426 | KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH); | 
|  | 427 | } else { | 
|  | 428 | struct dirent* entry; | 
|  | 429 |  | 
|  | 430 | while ((entry = readdir(dir))) { | 
|  | 431 | const char* name = entry->d_name; | 
|  | 432 |  | 
|  | 433 | if (!strcmp(name, ".") || !strcmp(name, "..")) | 
|  | 434 | continue; | 
|  | 435 |  | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 436 | // Look for "type" file in each subdirectory | 
|  | 437 | path.clear(); | 
|  | 438 | path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name); | 
|  | 439 | switch(readPowerSupplyType(path)) { | 
|  | 440 | case ANDROID_POWER_SUPPLY_TYPE_AC: | 
|  | 441 | case ANDROID_POWER_SUPPLY_TYPE_USB: | 
|  | 442 | case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: | 
|  | 443 | path.clear(); | 
|  | 444 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); | 
|  | 445 | if (access(path.string(), R_OK) == 0) | 
|  | 446 | mChargerNames.add(String8(name)); | 
|  | 447 | break; | 
|  | 448 |  | 
|  | 449 | case ANDROID_POWER_SUPPLY_TYPE_BATTERY: | 
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 450 | mBatteryDevicePresent = true; | 
|  | 451 |  | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 452 | if (mHealthdConfig->batteryStatusPath.isEmpty()) { | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 453 | path.clear(); | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 454 | path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH, | 
|  | 455 | name); | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 456 | if (access(path, R_OK) == 0) | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 457 | mHealthdConfig->batteryStatusPath = path; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 458 | } | 
|  | 459 |  | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 460 | if (mHealthdConfig->batteryHealthPath.isEmpty()) { | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 461 | path.clear(); | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 462 | path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH, | 
|  | 463 | name); | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 464 | if (access(path, R_OK) == 0) | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 465 | mHealthdConfig->batteryHealthPath = path; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 466 | } | 
|  | 467 |  | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 468 | if (mHealthdConfig->batteryPresentPath.isEmpty()) { | 
|  | 469 | path.clear(); | 
|  | 470 | path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH, | 
|  | 471 | name); | 
|  | 472 | if (access(path, R_OK) == 0) | 
|  | 473 | mHealthdConfig->batteryPresentPath = path; | 
|  | 474 | } | 
|  | 475 |  | 
|  | 476 | if (mHealthdConfig->batteryCapacityPath.isEmpty()) { | 
|  | 477 | path.clear(); | 
|  | 478 | path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH, | 
|  | 479 | name); | 
|  | 480 | if (access(path, R_OK) == 0) | 
|  | 481 | mHealthdConfig->batteryCapacityPath = path; | 
|  | 482 | } | 
|  | 483 |  | 
|  | 484 | if (mHealthdConfig->batteryVoltagePath.isEmpty()) { | 
|  | 485 | path.clear(); | 
|  | 486 | path.appendFormat("%s/%s/voltage_now", | 
|  | 487 | POWER_SUPPLY_SYSFS_PATH, name); | 
|  | 488 | if (access(path, R_OK) == 0) { | 
|  | 489 | mHealthdConfig->batteryVoltagePath = path; | 
|  | 490 | } else { | 
|  | 491 | path.clear(); | 
|  | 492 | path.appendFormat("%s/%s/batt_vol", | 
|  | 493 | POWER_SUPPLY_SYSFS_PATH, name); | 
|  | 494 | if (access(path, R_OK) == 0) | 
|  | 495 | mHealthdConfig->batteryVoltagePath = path; | 
|  | 496 | } | 
|  | 497 | } | 
|  | 498 |  | 
|  | 499 | if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) { | 
|  | 500 | path.clear(); | 
|  | 501 | path.appendFormat("%s/%s/current_now", | 
|  | 502 | POWER_SUPPLY_SYSFS_PATH, name); | 
|  | 503 | if (access(path, R_OK) == 0) | 
|  | 504 | mHealthdConfig->batteryCurrentNowPath = path; | 
|  | 505 | } | 
|  | 506 |  | 
| Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 507 | if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { | 
|  | 508 | path.clear(); | 
|  | 509 | path.appendFormat("%s/%s/current_avg", | 
|  | 510 | POWER_SUPPLY_SYSFS_PATH, name); | 
|  | 511 | if (access(path, R_OK) == 0) | 
|  | 512 | mHealthdConfig->batteryCurrentAvgPath = path; | 
|  | 513 | } | 
|  | 514 |  | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 515 | if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) { | 
|  | 516 | path.clear(); | 
|  | 517 | path.appendFormat("%s/%s/charge_counter", | 
|  | 518 | POWER_SUPPLY_SYSFS_PATH, name); | 
|  | 519 | if (access(path, R_OK) == 0) | 
|  | 520 | mHealthdConfig->batteryChargeCounterPath = path; | 
|  | 521 | } | 
|  | 522 |  | 
|  | 523 | if (mHealthdConfig->batteryTemperaturePath.isEmpty()) { | 
|  | 524 | path.clear(); | 
|  | 525 | path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH, | 
|  | 526 | name); | 
|  | 527 | if (access(path, R_OK) == 0) { | 
|  | 528 | mHealthdConfig->batteryTemperaturePath = path; | 
|  | 529 | } else { | 
|  | 530 | path.clear(); | 
|  | 531 | path.appendFormat("%s/%s/batt_temp", | 
|  | 532 | POWER_SUPPLY_SYSFS_PATH, name); | 
|  | 533 | if (access(path, R_OK) == 0) | 
|  | 534 | mHealthdConfig->batteryTemperaturePath = path; | 
|  | 535 | } | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | if (mHealthdConfig->batteryTechnologyPath.isEmpty()) { | 
|  | 539 | path.clear(); | 
|  | 540 | path.appendFormat("%s/%s/technology", | 
|  | 541 | POWER_SUPPLY_SYSFS_PATH, name); | 
|  | 542 | if (access(path, R_OK) == 0) | 
|  | 543 | mHealthdConfig->batteryTechnologyPath = path; | 
|  | 544 | } | 
|  | 545 |  | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 546 | break; | 
|  | 547 |  | 
|  | 548 | case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN: | 
|  | 549 | break; | 
|  | 550 | } | 
|  | 551 | } | 
|  | 552 | closedir(dir); | 
|  | 553 | } | 
|  | 554 |  | 
| Ruchi Kandoi | 42a981d | 2015-09-28 13:35:59 -0700 | [diff] [blame] | 555 | // Typically the case for devices which do not have a battery and | 
|  | 556 | // and are always plugged into AC mains. | 
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 557 | if (!mBatteryDevicePresent) { | 
| Todd Poynor | ebeb0c0 | 2014-09-23 14:54:24 -0700 | [diff] [blame] | 558 | KLOG_WARNING(LOG_TAG, "No battery devices found\n"); | 
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 559 | hc->periodic_chores_interval_fast = -1; | 
|  | 560 | hc->periodic_chores_interval_slow = -1; | 
| Ruchi Kandoi | 9cb3d3c | 2016-02-29 13:13:39 -0800 | [diff] [blame] | 561 | mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY; | 
|  | 562 | mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE; | 
|  | 563 | mAlwaysPluggedDevice = true; | 
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 564 | } else { | 
|  | 565 | if (mHealthdConfig->batteryStatusPath.isEmpty()) | 
|  | 566 | KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n"); | 
|  | 567 | if (mHealthdConfig->batteryHealthPath.isEmpty()) | 
|  | 568 | KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n"); | 
|  | 569 | if (mHealthdConfig->batteryPresentPath.isEmpty()) | 
|  | 570 | KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n"); | 
|  | 571 | if (mHealthdConfig->batteryCapacityPath.isEmpty()) | 
|  | 572 | KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n"); | 
|  | 573 | if (mHealthdConfig->batteryVoltagePath.isEmpty()) | 
|  | 574 | KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n"); | 
|  | 575 | if (mHealthdConfig->batteryTemperaturePath.isEmpty()) | 
|  | 576 | KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n"); | 
|  | 577 | if (mHealthdConfig->batteryTechnologyPath.isEmpty()) | 
|  | 578 | KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n"); | 
|  | 579 | } | 
| Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 580 |  | 
| Ruchi Kandoi | a78fc23 | 2014-07-10 15:06:21 -0700 | [diff] [blame] | 581 | if (property_get("ro.boot.fake_battery", pval, NULL) > 0 | 
|  | 582 | && strtol(pval, NULL, 10) != 0) { | 
|  | 583 | mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY; | 
|  | 584 | mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE; | 
|  | 585 | } | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 586 | } | 
|  | 587 |  | 
|  | 588 | }; // namespace android |