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