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