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 (at) 2.0-service.crosshatch" 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 <health2/Health.h> 23 #include <health2/service.h> 24 #include <healthd/healthd.h> 25 #include <hidl/HidlTransportSupport.h> 26 #include <pixelhealth/BatteryMetricsLogger.h> 27 #include <pixelhealth/CycleCountBackupRestore.h> 28 #include <pixelhealth/DeviceHealth.h> 29 #include <pixelhealth/LowBatteryShutdownMetrics.h> 30 31 #include <fstream> 32 #include <iomanip> 33 #include <string> 34 #include <vector> 35 36 #include "BatteryRechargingControl.h" 37 38 namespace { 39 40 using android::hardware::health::V2_0::DiskStats; 41 using android::hardware::health::V2_0::StorageAttribute; 42 using android::hardware::health::V2_0::StorageInfo; 43 using hardware::google::pixel::health::BatteryMetricsLogger; 44 using hardware::google::pixel::health::CycleCountBackupRestore; 45 using hardware::google::pixel::health::DeviceHealth; 46 using hardware::google::pixel::health::LowBatteryShutdownMetrics; 47 using ::device::google::crosshatch::health::BatteryRechargingControl; 48 49 constexpr char kBatteryResistance[] = "/sys/class/power_supply/maxfg/resistance"; 50 constexpr char kBatteryOCV[] = "/sys/class/power_supply/maxfg/voltage_ocv"; 51 constexpr char kVoltageAvg[] = "/sys/class/power_supply/maxfg/voltage_avg"; 52 53 static BatteryRechargingControl battRechargingControl; 54 static BatteryMetricsLogger battMetricsLogger(kBatteryResistance, kBatteryOCV); 55 static LowBatteryShutdownMetrics shutdownMetrics(kVoltageAvg); 56 static CycleCountBackupRestore ccBackupRestoreBMS( 57 8, "/sys/class/power_supply/bms/device/cycle_counts_bins", 58 "/persist/battery/qcom_cycle_counts_bins"); 59 static CycleCountBackupRestore ccBackupRestoreMAX( 60 10, "/sys/class/power_supply/maxfg/cycle_counts_bins", 61 "/persist/battery/max_cycle_counts_bins", "/sys/class/power_supply/maxfg/serial_number"); 62 static DeviceHealth deviceHealth; 63 64 #define UFS_DIR "/sys/devices/platform/soc/1d84000.ufshc" 65 const std::string kUfsHealthEol{UFS_DIR "/health/eol"}; 66 const std::string kUfsHealthLifetimeA{UFS_DIR "/health/lifetimeA"}; 67 const std::string kUfsHealthLifetimeB{UFS_DIR "/health/lifetimeB"}; 68 const std::string kUfsVersion{UFS_DIR "/version"}; 69 const std::string kDiskStatsFile{"/sys/block/sda/stat"}; 70 const std::string kUFSName{"UFS0"}; 71 72 std::ifstream assert_open(const std::string &path) { 73 std::ifstream stream(path); 74 if (!stream.is_open()) { 75 LOG(FATAL) << "Cannot read " << path; 76 } 77 return stream; 78 } 79 80 template <typename T> 81 void read_value_from_file(const std::string &path, T *field) { 82 auto stream = assert_open(path); 83 stream.unsetf(std::ios_base::basefield); 84 stream >> *field; 85 } 86 87 void read_ufs_version(StorageInfo *info) { 88 uint64_t value; 89 read_value_from_file(kUfsVersion, &value); 90 std::stringstream ss; 91 ss << "ufs " << std::hex << value; 92 info->version = ss.str(); 93 } 94 95 void fill_ufs_storage_attribute(StorageAttribute *attr) { 96 attr->isInternal = true; 97 attr->isBootDevice = true; 98 attr->name = kUFSName; 99 } 100 101 } // anonymous namespace 102 103 void healthd_board_init(struct healthd_config *config) { 104 using ::device::google::crosshatch::health::kChargerStatus; 105 106 ccBackupRestoreBMS.Restore(); 107 ccBackupRestoreMAX.Restore(); 108 109 config->batteryStatusPath = kChargerStatus.c_str(); 110 } 111 112 int healthd_board_battery_update(struct android::BatteryProperties *props) { 113 battRechargingControl.updateBatteryProperties(props); 114 deviceHealth.update(props); 115 battMetricsLogger.logBatteryProperties(props); 116 shutdownMetrics.logShutdownVoltage(props); 117 ccBackupRestoreBMS.Backup(props->batteryLevel); 118 ccBackupRestoreMAX.Backup(props->batteryLevel); 119 if (!android::base::WriteStringToFile(std::to_string(props->batteryLevel), 120 "/sys/class/power_supply/wireless/capacity")) 121 LOG(INFO) << "Unable to write battery level to wireless capacity"; 122 return 0; 123 } 124 125 void get_storage_info(std::vector<StorageInfo> &vec_storage_info) { 126 vec_storage_info.resize(1); 127 StorageInfo *storage_info = &vec_storage_info[0]; 128 fill_ufs_storage_attribute(&storage_info->attr); 129 130 read_ufs_version(storage_info); 131 read_value_from_file(kUfsHealthEol, &storage_info->eol); 132 read_value_from_file(kUfsHealthLifetimeA, &storage_info->lifetimeA); 133 read_value_from_file(kUfsHealthLifetimeB, &storage_info->lifetimeB); 134 return; 135 } 136 137 void get_disk_stats(std::vector<DiskStats> &vec_stats) { 138 vec_stats.resize(1); 139 DiskStats *stats = &vec_stats[0]; 140 fill_ufs_storage_attribute(&stats->attr); 141 142 auto stream = assert_open(kDiskStatsFile); 143 // Regular diskstats entries 144 stream >> stats->reads >> stats->readMerges >> stats->readSectors >> stats->readTicks >> 145 stats->writes >> stats->writeMerges >> stats->writeSectors >> stats->writeTicks >> 146 stats->ioInFlight >> stats->ioTicks >> stats->ioInQueue; 147 return; 148 } 149 150 int main(void) { 151 return health_service_main(); 152 } 153