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