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