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