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