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