1 /*
2 * Copyright (C) 2018 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 #define LOG_TAG "android.hardware.health@2.1-impl-redbull"
17 #include <android-base/logging.h>
18
19 #include <android-base/file.h>
20 #include <android-base/parseint.h>
21 #include <android-base/strings.h>
22 #include <android/hardware/health/2.0/types.h>
23 #include <health2impl/Health.h>
24 #include <health/utils.h>
25 #include <hal_conversion.h>
26
27 #include <pixelhealth/BatteryDefender.h>
28 #include <pixelhealth/BatteryMetricsLogger.h>
29 #include <pixelhealth/BatteryThermalControl.h>
30 #include <pixelhealth/DeviceHealth.h>
31 #include <pixelhealth/LowBatteryShutdownMetrics.h>
32
33 #include <fstream>
34 #include <iomanip>
35 #include <string>
36 #include <vector>
37
38 namespace {
39
40 using namespace std::literals;
41
42 using android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo;
43 using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
44 using android::hardware::health::V2_0::DiskStats;
45 using android::hardware::health::V2_0::StorageAttribute;
46 using android::hardware::health::V2_0::StorageInfo;
47 using android::hardware::health::V2_0::Result;
48 using ::android::hardware::health::V2_1::IHealth;
49 using android::hardware::health::InitHealthdConfig;
50
51 using hardware::google::pixel::health::BatteryDefender;
52 using hardware::google::pixel::health::BatteryMetricsLogger;
53 using hardware::google::pixel::health::BatteryThermalControl;
54 using hardware::google::pixel::health::DeviceHealth;
55 using hardware::google::pixel::health::LowBatteryShutdownMetrics;
56
57 #define FG_DIR "/sys/class/power_supply"
58 constexpr char kBatteryResistance[] {FG_DIR "/bms/resistance"};
59 constexpr char kBatteryOCV[] {FG_DIR "/bms/voltage_ocv"};
60 constexpr char kVoltageAvg[] {FG_DIR "/battery/voltage_now"};
61
62 #define WLC_DIR "/sys/class/power_supply/wireless"
63
64 static BatteryDefender battDefender(WLC_DIR "/present");
65 static BatteryThermalControl battThermalControl(
66 "sys/devices/virtual/thermal/tz-by-name/soc/mode");
67 static BatteryMetricsLogger battMetricsLogger(kBatteryResistance, kBatteryOCV);
68 static LowBatteryShutdownMetrics shutdownMetrics(kVoltageAvg);
69 static DeviceHealth deviceHealth;
70
71 #define UFS_DIR "/dev/sys/block/bootdevice"
72 constexpr char kUfsHealthEol[]{UFS_DIR "/health_descriptor/eol_info"};
73 constexpr char kUfsHealthLifetimeA[]{UFS_DIR "/health_descriptor/life_time_estimation_a"};
74 constexpr char kUfsHealthLifetimeB[]{UFS_DIR "/health_descriptor/life_time_estimation_b"};
75 constexpr char kUfsVersion[]{UFS_DIR "/device_descriptor/specification_version"};
76 constexpr char kDiskStatsFile[]{"/sys/block/sda/stat"};
77 constexpr char kUFSName[]{"UFS0"};
78
79 constexpr char kTCPMPSYName[]{"tcpm-source-psy-usbpd0"};
80
81 static bool needs_wlc_updates = false;
82 constexpr char kWlcCapacity[]{WLC_DIR "/capacity"};
83
assert_open(const std::string & path)84 std::ifstream assert_open(const std::string &path) {
85 std::ifstream stream(path);
86 if (!stream.is_open()) {
87 LOG(WARNING) << "Cannot read " << path;
88 }
89 return stream;
90 }
91
92 template <typename T>
read_value_from_file(const std::string & path,T * field)93 void read_value_from_file(const std::string &path, T *field) {
94 auto stream = assert_open(path);
95 stream.unsetf(std::ios_base::basefield);
96 stream >> *field;
97 }
98
read_ufs_version(StorageInfo * info)99 void read_ufs_version(StorageInfo *info) {
100 uint64_t value;
101 read_value_from_file(kUfsVersion, &value);
102 std::stringstream ss;
103 ss << "ufs " << std::hex << value;
104 info->version = ss.str();
105 }
106
fill_ufs_storage_attribute(StorageAttribute * attr)107 void fill_ufs_storage_attribute(StorageAttribute *attr) {
108 attr->isInternal = true;
109 attr->isBootDevice = true;
110 attr->name = kUFSName;
111 }
112
FileExists(const std::string & filename)113 static bool FileExists(const std::string &filename) {
114 struct stat buffer;
115
116 return stat(filename.c_str(), &buffer) == 0;
117 }
118
private_healthd_board_init(struct healthd_config * hc)119 void private_healthd_board_init(struct healthd_config *hc) {
120 hc->ignorePowerSupplyNames.push_back(android::String8(kTCPMPSYName));
121 needs_wlc_updates = FileExists(kWlcCapacity);
122 if (needs_wlc_updates == false) {
123 battDefender.setWirelessNotSupported();
124 }
125 }
126
private_healthd_board_battery_update(struct android::BatteryProperties * props)127 int private_healthd_board_battery_update(struct android::BatteryProperties *props) {
128 deviceHealth.update(props);
129 battThermalControl.updateThermalState(props);
130 battMetricsLogger.logBatteryProperties(props);
131 shutdownMetrics.logShutdownVoltage(props);
132 battDefender.update(props);
133
134 if (needs_wlc_updates &&
135 !android::base::WriteStringToFile(std::to_string(props->batteryLevel), kWlcCapacity))
136 LOG(INFO) << "Unable to write battery level to wireless capacity";
137
138 return 0;
139 }
140
private_get_storage_info(std::vector<StorageInfo> & vec_storage_info)141 void private_get_storage_info(std::vector<StorageInfo> &vec_storage_info) {
142 vec_storage_info.resize(1);
143 StorageInfo *storage_info = &vec_storage_info[0];
144 fill_ufs_storage_attribute(&storage_info->attr);
145
146 read_ufs_version(storage_info);
147 read_value_from_file(kUfsHealthEol, &storage_info->eol);
148 read_value_from_file(kUfsHealthLifetimeA, &storage_info->lifetimeA);
149 read_value_from_file(kUfsHealthLifetimeB, &storage_info->lifetimeB);
150 return;
151 }
152
private_get_disk_stats(std::vector<DiskStats> & vec_stats)153 void private_get_disk_stats(std::vector<DiskStats> &vec_stats) {
154 vec_stats.resize(1);
155 DiskStats *stats = &vec_stats[0];
156 fill_ufs_storage_attribute(&stats->attr);
157
158 auto stream = assert_open(kDiskStatsFile);
159 // Regular diskstats entries
160 stream >> stats->reads >> stats->readMerges >> stats->readSectors >>
161 stats->readTicks >> stats->writes >> stats->writeMerges >>
162 stats->writeSectors >> stats->writeTicks >> stats->ioInFlight >>
163 stats->ioTicks >> stats->ioInQueue;
164 return;
165 }
166 } // anonymous namespace
167
168 namespace android {
169 namespace hardware {
170 namespace health {
171 namespace V2_1 {
172 namespace implementation {
173 class HealthImpl : public Health {
174 public:
HealthImpl(std::unique_ptr<healthd_config> && config)175 HealthImpl(std::unique_ptr<healthd_config>&& config)
176 : Health(std::move(config)) {}
177
178 Return<void> getStorageInfo(getStorageInfo_cb _hidl_cb) override;
179 Return<void> getDiskStats(getDiskStats_cb _hidl_cb) override;
180
181 protected:
182 void UpdateHealthInfo(HealthInfo* health_info) override;
183
184 };
185
UpdateHealthInfo(HealthInfo * health_info)186 void HealthImpl::UpdateHealthInfo(HealthInfo* health_info) {
187 struct BatteryProperties props;
188 convertFromHealthInfo(health_info->legacy.legacy, &props);
189 private_healthd_board_battery_update(&props);
190 convertToHealthInfo(&props, health_info->legacy.legacy);
191 }
192
getStorageInfo(getStorageInfo_cb _hidl_cb)193 Return<void> HealthImpl::getStorageInfo(getStorageInfo_cb _hidl_cb)
194 {
195 std::vector<struct StorageInfo> info;
196 private_get_storage_info(info);
197 hidl_vec<struct StorageInfo> info_vec(info);
198 if (!info.size()) {
199 _hidl_cb(Result::NOT_SUPPORTED, info_vec);
200 } else {
201 _hidl_cb(Result::SUCCESS, info_vec);
202 }
203 return Void();
204 }
205
getDiskStats(getDiskStats_cb _hidl_cb)206 Return<void> HealthImpl::getDiskStats(getDiskStats_cb _hidl_cb)
207 {
208 std::vector<struct DiskStats> stats;
209 private_get_disk_stats(stats);
210 hidl_vec<struct DiskStats> stats_vec(stats);
211 if (!stats.size()) {
212 _hidl_cb(Result::NOT_SUPPORTED, stats_vec);
213 } else {
214 _hidl_cb(Result::SUCCESS, stats_vec);
215 }
216 return Void();
217 }
218
219 } // namespace implementation
220 } // namespace V2_1
221 } // namespace health
222 } // namespace hardware
223 } // namespace android
224
HIDL_FETCH_IHealth(const char * instance)225 extern "C" IHealth* HIDL_FETCH_IHealth(const char* instance) {
226 using ::android::hardware::health::V2_1::implementation::HealthImpl;
227 if (instance != "default"sv) {
228 return nullptr;
229 }
230 auto config = std::make_unique<healthd_config>();
231 InitHealthdConfig(config.get());
232
233 private_healthd_board_init(config.get());
234
235 return new HealthImpl(std::move(config));
236 }
237