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