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