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