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