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.0-service.crosshatch"
17 #include <android-base/file.h>
18 #include <android-base/logging.h>
19 #include <android-base/parseint.h>
20 #include <android-base/strings.h>
21 #include <health2/Health.h>
22 #include <health2/service.h>
23 #include <healthd/healthd.h>
24 #include <hidl/HidlTransportSupport.h>
25 #include <pixelhealth/BatteryDefender.h>
26 #include <pixelhealth/BatteryMetricsLogger.h>
27 #include <pixelhealth/BatteryThermalControl.h>
28 #include <pixelhealth/CycleCountBackupRestore.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 #include "BatteryRechargingControl.h"
38
39 namespace {
40
41 using android::hardware::health::V2_0::DiskStats;
42 using android::hardware::health::V2_0::StorageAttribute;
43 using android::hardware::health::V2_0::StorageInfo;
44 using ::device::google::crosshatch::health::BatteryRechargingControl;
45 using hardware::google::pixel::health::BatteryDefender;
46 using hardware::google::pixel::health::BatteryMetricsLogger;
47 using hardware::google::pixel::health::BatteryThermalControl;
48 using hardware::google::pixel::health::CycleCountBackupRestore;
49 using hardware::google::pixel::health::DeviceHealth;
50 using hardware::google::pixel::health::LowBatteryShutdownMetrics;
51
52 constexpr char kBatteryResistance[] = "/sys/class/power_supply/maxfg/resistance";
53 constexpr char kBatteryOCV[] = "/sys/class/power_supply/maxfg/voltage_ocv";
54 constexpr char kVoltageAvg[] = "/sys/class/power_supply/maxfg/voltage_avg";
55
56 #define WLC_DIR "/sys/class/power_supply/wireless"
57
58 static BatteryDefender battDefender(WLC_DIR "/online");
59 static BatteryRechargingControl battRechargingControl;
60 static BatteryThermalControl battThermalControl("sys/devices/virtual/thermal/tz-by-name/soc/mode");
61 static BatteryMetricsLogger battMetricsLogger(kBatteryResistance, kBatteryOCV);
62 static LowBatteryShutdownMetrics shutdownMetrics(kVoltageAvg);
63 static CycleCountBackupRestore ccBackupRestoreBMS(
64 8, "/sys/class/power_supply/bms/device/cycle_counts_bins",
65 "/mnt/vendor/persist/battery/qcom_cycle_counts_bins");
66 static CycleCountBackupRestore ccBackupRestoreMAX(
67 10, "/sys/class/power_supply/maxfg/cycle_counts_bins",
68 "/mnt/vendor/persist/battery/max_cycle_counts_bins",
69 "/sys/class/power_supply/maxfg/serial_number");
70 static DeviceHealth deviceHealth;
71
72 #define UFS_DIR "/sys/devices/platform/soc/1d84000.ufshc"
73 const std::string kUfsHealthEol{UFS_DIR "/health/eol"};
74 const std::string kUfsHealthLifetimeA{UFS_DIR "/health/lifetimeA"};
75 const std::string kUfsHealthLifetimeB{UFS_DIR "/health/lifetimeB"};
76 const std::string kUfsVersion{UFS_DIR "/version"};
77 const std::string kDiskStatsFile{"/sys/block/sda/stat"};
78 const std::string kUFSName{"UFS0"};
79
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(FATAL) << "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
112 } // anonymous namespace
113
FileExists(const std::string & filename)114 static bool FileExists(const std::string &filename) {
115 struct stat buffer;
116
117 return stat(filename.c_str(), &buffer) == 0;
118 }
119
healthd_board_init(struct healthd_config * config)120 void healthd_board_init(struct healthd_config *config) {
121 using ::device::google::crosshatch::health::kChargerStatus;
122
123 ccBackupRestoreBMS.Restore();
124 ccBackupRestoreMAX.Restore();
125
126 config->batteryStatusPath = kChargerStatus.c_str();
127
128 needs_wlc_updates = FileExists(kWlcCapacity);
129 }
130
healthd_board_battery_update(struct android::BatteryProperties * props)131 int healthd_board_battery_update(struct android::BatteryProperties *props) {
132 battRechargingControl.updateBatteryProperties(props);
133 deviceHealth.update(props);
134 battThermalControl.updateThermalState(props);
135 battMetricsLogger.logBatteryProperties(props);
136 shutdownMetrics.logShutdownVoltage(props);
137 ccBackupRestoreBMS.Backup(props->batteryLevel);
138 ccBackupRestoreMAX.Backup(props->batteryLevel);
139 battDefender.update(props);
140
141 if (needs_wlc_updates &&
142 !android::base::WriteStringToFile(std::to_string(props->batteryLevel), kWlcCapacity))
143 LOG(INFO) << "Unable to write battery level to wireless capacity";
144
145 return 0;
146 }
147
get_storage_info(std::vector<StorageInfo> & vec_storage_info)148 void get_storage_info(std::vector<StorageInfo> &vec_storage_info) {
149 vec_storage_info.resize(1);
150 StorageInfo *storage_info = &vec_storage_info[0];
151 fill_ufs_storage_attribute(&storage_info->attr);
152
153 read_ufs_version(storage_info);
154 read_value_from_file(kUfsHealthEol, &storage_info->eol);
155 read_value_from_file(kUfsHealthLifetimeA, &storage_info->lifetimeA);
156 read_value_from_file(kUfsHealthLifetimeB, &storage_info->lifetimeB);
157 return;
158 }
159
get_disk_stats(std::vector<DiskStats> & vec_stats)160 void get_disk_stats(std::vector<DiskStats> &vec_stats) {
161 vec_stats.resize(1);
162 DiskStats *stats = &vec_stats[0];
163 fill_ufs_storage_attribute(&stats->attr);
164
165 auto stream = assert_open(kDiskStatsFile);
166 // Regular diskstats entries
167 stream >> stats->reads >> stats->readMerges >> stats->readSectors >> stats->readTicks >>
168 stats->writes >> stats->writeMerges >> stats->writeSectors >> stats->writeTicks >>
169 stats->ioInFlight >> stats->ioTicks >> stats->ioInQueue;
170 return;
171 }
172
main(void)173 int main(void) {
174 return health_service_main();
175 }
176