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