• 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.0-service.marlin"
17 #include <android-base/logging.h>
18 
19 #include <healthd/healthd.h>
20 #include <health2/Health.h>
21 #include <health2/service.h>
22 #include <hidl/HidlTransportSupport.h>
23 
24 #include <android-base/file.h>
25 #include <android-base/strings.h>
26 
27 #include <vector>
28 #include <string>
29 
30 #include <sys/stat.h>
31 
32 #include "CycleCountBackupRestore.h"
33 #include "LearnedCapacityBackupRestore.h"
34 
35 using android::hardware::health::V2_0::StorageInfo;
36 using android::hardware::health::V2_0::DiskStats;
37 using ::device::google::marlin::health::CycleCountBackupRestore;
38 using ::device::google::marlin::health::LearnedCapacityBackupRestore;
39 
40 static constexpr int kBackupTrigger = 20;
41 static constexpr size_t kDiskStatsSize = 11;
42 static constexpr char kUFSHealthFile[] = "/sys/devices/soc/624000.ufshc/health";
43 static constexpr char kUFSHealthVersionFile[] = "/sys/kernel/debug/ufshcd0/show_hba";
44 static constexpr char kUFSName[] = "UFS0";
45 static constexpr char kDiskStatsFile[] = "/sys/block/sda/stat";
46 
47 static CycleCountBackupRestore ccBackupRestore;
48 static LearnedCapacityBackupRestore lcBackupRestore;
49 
cycle_count_backup(int battery_level)50 int cycle_count_backup(int battery_level)
51 {
52     static int saved_soc = 0;
53     static int soc_inc = 0;
54     static bool is_first = true;
55 
56     if (is_first) {
57         is_first = false;
58         saved_soc = battery_level;
59         return 0;
60     }
61 
62     if (battery_level > saved_soc) {
63         soc_inc += battery_level - saved_soc;
64     }
65 
66     saved_soc = battery_level;
67 
68     if (soc_inc >= kBackupTrigger) {
69         ccBackupRestore.Backup();
70         soc_inc = 0;
71     }
72     return 0;
73 }
74 
75 // See : hardware/interfaces/health/2.0/README
76 
healthd_board_init(struct healthd_config *)77 void healthd_board_init(struct healthd_config*)
78 {
79     ccBackupRestore.Restore();
80     lcBackupRestore.Restore();
81 }
82 
healthd_board_battery_update(struct android::BatteryProperties * props)83 int healthd_board_battery_update(struct android::BatteryProperties *props)
84 {
85     cycle_count_backup(props->batteryLevel);
86     lcBackupRestore.Backup();
87     return 0;
88 }
89 
get_storage_info(std::vector<StorageInfo> & vec_storage_info)90 void get_storage_info(std::vector<StorageInfo>& vec_storage_info) {
91     StorageInfo storage_info = {};
92     std::string buffer, version;
93 
94     storage_info.attr.isInternal = true;
95     storage_info.attr.isBootDevice = true;
96     storage_info.attr.name = std::string(kUFSName);
97 
98     if (!android::base::ReadFileToString(std::string(kUFSHealthVersionFile), &version)) {
99         return;
100     }
101 
102     std::vector<std::string> lines = android::base::Split(version, "\n");
103     if (lines.empty()) {
104         return;
105     }
106 
107     char rev[8];
108     if (sscanf(lines[6].c_str(), "ufs version: 0x%7s\n", rev) < 1) {
109         return;
110     }
111 
112     storage_info.version = "ufs " + std::string(rev);
113 
114     if (!android::base::ReadFileToString(std::string(kUFSHealthFile), &buffer)) {
115         return;
116     }
117 
118     lines = android::base::Split(buffer, "\n");
119     if (lines.empty()) {
120         return;
121     }
122 
123     for (const auto& line : lines) {
124         char token[32];
125         uint16_t val;
126         int ret;
127         if ((ret = sscanf(line.c_str(),
128                    "Health Descriptor[Byte offset 0x%*d]: %31s = 0x%hx",
129                    token, &val)) < 2) {
130             continue;
131         }
132 
133         if (std::string(token) == "bPreEOLInfo") {
134             storage_info.eol = val;
135         } else if (std::string(token) == "bDeviceLifeTimeEstA") {
136             storage_info.lifetimeA = val;
137         } else if (std::string(token) == "bDeviceLifeTimeEstB") {
138             storage_info.lifetimeB = val;
139         }
140     }
141 
142     vec_storage_info.resize(1);
143     vec_storage_info[0] = storage_info;
144     return;
145 }
146 
147 
get_disk_stats(std::vector<DiskStats> & vec_stats)148 void get_disk_stats(std::vector<DiskStats>& vec_stats) {
149     DiskStats stats = {};
150 
151     stats.attr.isInternal = true;
152     stats.attr.isBootDevice = true;
153     stats.attr.name = std::string(kUFSName);
154 
155     std::string buffer;
156     if (!android::base::ReadFileToString(std::string(kDiskStatsFile), &buffer)) {
157         LOG(ERROR) << kDiskStatsFile << ": ReadFileToString failed.";
158         return;
159     }
160 
161     // Regular diskstats entries
162     std::stringstream ss(buffer);
163     for (uint i = 0; i < kDiskStatsSize; i++) {
164         ss >> *(reinterpret_cast<uint64_t*>(&stats) + i);
165     }
166     vec_stats.resize(1);
167     vec_stats[0] = stats;
168 
169     return;
170 }
171 
main(void)172 int main(void) {
173     return health_service_main();
174 }
175