blob: c0c5a404c9b91d4cb2f347a193f057de98130deb [file] [log] [blame]
Hridya Valsaraju2b520832017-12-20 13:25:29 -08001/*
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 */
Hridya Valsarajud3e3d722017-12-11 17:31:00 -080016#define LOG_TAG "android.hardware.health@2.0-impl"
17#include <android-base/logging.h>
18
Yifan Hong81b28332018-04-04 15:28:19 -070019#include <android-base/file.h>
Hridya Valsarajud3e3d722017-12-11 17:31:00 -080020#include <health2/Health.h>
21
Hridya Valsaraju1bd37722018-01-12 10:25:59 -080022#include <hal_conversion.h>
Hridya Valsarajud3e3d722017-12-11 17:31:00 -080023#include <hidl/HidlTransportSupport.h>
24
25extern void healthd_battery_update_internal(bool);
26
27namespace android {
28namespace hardware {
29namespace health {
30namespace V2_0 {
31namespace implementation {
32
33sp<Health> Health::instance_;
34
35Health::Health(struct healthd_config* c) {
36 // TODO(b/69268160): remove when libhealthd is removed.
37 healthd_board_init(c);
38 battery_monitor_ = std::make_unique<BatteryMonitor>();
39 battery_monitor_->init(c);
40}
41
42// Methods from IHealth follow.
43Return<Result> Health::registerCallback(const sp<IHealthInfoCallback>& callback) {
44 if (callback == nullptr) {
45 return Result::SUCCESS;
46 }
47
48 {
Yifan Honga46c0da2018-10-02 14:06:51 -070049 std::lock_guard<decltype(callbacks_lock_)> lock(callbacks_lock_);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -080050 callbacks_.push_back(callback);
51 // unlock
52 }
53
54 auto linkRet = callback->linkToDeath(this, 0u /* cookie */);
55 if (!linkRet.withDefault(false)) {
56 LOG(WARNING) << __func__ << "Cannot link to death: "
57 << (linkRet.isOk() ? "linkToDeath returns false" : linkRet.description());
58 // ignore the error
59 }
60
Yifan Honge9fc2352018-10-02 14:11:35 -070061 return updateAndNotify(callback);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -080062}
63
64bool Health::unregisterCallbackInternal(const sp<IBase>& callback) {
65 if (callback == nullptr) return false;
66
67 bool removed = false;
Yifan Honga46c0da2018-10-02 14:06:51 -070068 std::lock_guard<decltype(callbacks_lock_)> lock(callbacks_lock_);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -080069 for (auto it = callbacks_.begin(); it != callbacks_.end();) {
70 if (interfacesEqual(*it, callback)) {
71 it = callbacks_.erase(it);
72 removed = true;
73 } else {
74 ++it;
75 }
76 }
77 (void)callback->unlinkToDeath(this).isOk(); // ignore errors
78 return removed;
79}
80
81Return<Result> Health::unregisterCallback(const sp<IHealthInfoCallback>& callback) {
82 return unregisterCallbackInternal(callback) ? Result::SUCCESS : Result::NOT_FOUND;
83}
84
85template <typename T>
86void getProperty(const std::unique_ptr<BatteryMonitor>& monitor, int id, T defaultValue,
87 const std::function<void(Result, T)>& callback) {
88 struct BatteryProperty prop;
89 T ret = defaultValue;
90 Result result = Result::SUCCESS;
91 status_t err = monitor->getProperty(static_cast<int>(id), &prop);
92 if (err != OK) {
93 LOG(DEBUG) << "getProperty(" << id << ")"
94 << " fails: (" << err << ") " << strerror(-err);
95 } else {
96 ret = static_cast<T>(prop.valueInt64);
97 }
98 switch (err) {
99 case OK:
100 result = Result::SUCCESS;
101 break;
102 case NAME_NOT_FOUND:
103 result = Result::NOT_SUPPORTED;
104 break;
105 default:
106 result = Result::UNKNOWN;
107 break;
108 }
109 callback(result, static_cast<T>(ret));
110}
111
112Return<void> Health::getChargeCounter(getChargeCounter_cb _hidl_cb) {
Yifan Hong81874af2018-01-17 10:59:12 -0800113 getProperty<int32_t>(battery_monitor_, BATTERY_PROP_CHARGE_COUNTER, 0, _hidl_cb);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800114 return Void();
115}
116
117Return<void> Health::getCurrentNow(getCurrentNow_cb _hidl_cb) {
Yifan Hong81874af2018-01-17 10:59:12 -0800118 getProperty<int32_t>(battery_monitor_, BATTERY_PROP_CURRENT_NOW, 0, _hidl_cb);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800119 return Void();
120}
121
122Return<void> Health::getCurrentAverage(getCurrentAverage_cb _hidl_cb) {
Yifan Hong81874af2018-01-17 10:59:12 -0800123 getProperty<int32_t>(battery_monitor_, BATTERY_PROP_CURRENT_AVG, 0, _hidl_cb);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800124 return Void();
125}
126
127Return<void> Health::getCapacity(getCapacity_cb _hidl_cb) {
Yifan Hong81874af2018-01-17 10:59:12 -0800128 getProperty<int32_t>(battery_monitor_, BATTERY_PROP_CAPACITY, 0, _hidl_cb);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800129 return Void();
130}
131
132Return<void> Health::getEnergyCounter(getEnergyCounter_cb _hidl_cb) {
Yifan Hong81874af2018-01-17 10:59:12 -0800133 getProperty<int64_t>(battery_monitor_, BATTERY_PROP_ENERGY_COUNTER, 0, _hidl_cb);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800134 return Void();
135}
136
137Return<void> Health::getChargeStatus(getChargeStatus_cb _hidl_cb) {
138 getProperty(battery_monitor_, BATTERY_PROP_BATTERY_STATUS, BatteryStatus::UNKNOWN, _hidl_cb);
139 return Void();
140}
141
142Return<Result> Health::update() {
143 if (!healthd_mode_ops || !healthd_mode_ops->battery_update) {
144 LOG(WARNING) << "health@2.0: update: not initialized. "
Hridya Valsarajubf0b9fa2018-09-28 10:44:02 -0700145 << "update() should not be called in charger";
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800146 return Result::UNKNOWN;
147 }
148
149 // Retrieve all information and call healthd_mode_ops->battery_update, which calls
150 // notifyListeners.
Yifan Hong9521b9d2019-10-07 10:47:02 -0700151 battery_monitor_->updateValues();
152 struct BatteryProperties props = getBatteryProperties(battery_monitor_.get());
153 bool log = healthd_board_battery_update(&props);
154 if (log) {
155 battery_monitor_->logValues();
156 }
157 healthd_mode_ops->battery_update(&props);
158 bool chargerOnline = battery_monitor_->isChargerOnline();
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800159
160 // adjust uevent / wakealarm periods
161 healthd_battery_update_internal(chargerOnline);
162
163 return Result::SUCCESS;
164}
165
Yifan Honge9fc2352018-10-02 14:11:35 -0700166Return<Result> Health::updateAndNotify(const sp<IHealthInfoCallback>& callback) {
167 std::lock_guard<decltype(callbacks_lock_)> lock(callbacks_lock_);
168 std::vector<sp<IHealthInfoCallback>> storedCallbacks{std::move(callbacks_)};
169 callbacks_.clear();
170 if (callback != nullptr) {
171 callbacks_.push_back(callback);
172 }
173 Return<Result> result = update();
174 callbacks_ = std::move(storedCallbacks);
175 return result;
176}
177
Hridya Valsarajud31932a2018-01-17 23:09:24 -0800178void Health::notifyListeners(HealthInfo* healthInfo) {
179 std::vector<StorageInfo> info;
180 get_storage_info(info);
181
182 std::vector<DiskStats> stats;
183 get_disk_stats(stats);
184
185 int32_t currentAvg = 0;
186
187 struct BatteryProperty prop;
188 status_t ret = battery_monitor_->getProperty(BATTERY_PROP_CURRENT_AVG, &prop);
189 if (ret == OK) {
190 currentAvg = static_cast<int32_t>(prop.valueInt64);
191 }
192
193 healthInfo->batteryCurrentAverage = currentAvg;
194 healthInfo->diskStats = stats;
195 healthInfo->storageInfos = info;
196
Yifan Honga46c0da2018-10-02 14:06:51 -0700197 std::lock_guard<decltype(callbacks_lock_)> lock(callbacks_lock_);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800198 for (auto it = callbacks_.begin(); it != callbacks_.end();) {
Hridya Valsarajud31932a2018-01-17 23:09:24 -0800199 auto ret = (*it)->healthInfoChanged(*healthInfo);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800200 if (!ret.isOk() && ret.isDeadObject()) {
201 it = callbacks_.erase(it);
202 } else {
203 ++it;
204 }
205 }
206}
207
208Return<void> Health::debug(const hidl_handle& handle, const hidl_vec<hidl_string>&) {
209 if (handle != nullptr && handle->numFds >= 1) {
210 int fd = handle->data[0];
211 battery_monitor_->dumpState(fd);
Yifan Hong81b28332018-04-04 15:28:19 -0700212
213 getHealthInfo([fd](auto res, const auto& info) {
214 android::base::WriteStringToFd("\ngetHealthInfo -> ", fd);
215 if (res == Result::SUCCESS) {
216 android::base::WriteStringToFd(toString(info), fd);
217 } else {
218 android::base::WriteStringToFd(toString(res), fd);
219 }
220 android::base::WriteStringToFd("\n", fd);
221 });
222
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800223 fsync(fd);
224 }
225 return Void();
226}
227
Hridya Valsaraju2b520832017-12-20 13:25:29 -0800228Return<void> Health::getStorageInfo(getStorageInfo_cb _hidl_cb) {
229 std::vector<struct StorageInfo> info;
230 get_storage_info(info);
231 hidl_vec<struct StorageInfo> info_vec(info);
232 if (!info.size()) {
233 _hidl_cb(Result::NOT_SUPPORTED, info_vec);
234 } else {
235 _hidl_cb(Result::SUCCESS, info_vec);
236 }
237 return Void();
238}
239
240Return<void> Health::getDiskStats(getDiskStats_cb _hidl_cb) {
241 std::vector<struct DiskStats> stats;
242 get_disk_stats(stats);
243 hidl_vec<struct DiskStats> stats_vec(stats);
244 if (!stats.size()) {
245 _hidl_cb(Result::NOT_SUPPORTED, stats_vec);
246 } else {
247 _hidl_cb(Result::SUCCESS, stats_vec);
248 }
249 return Void();
250}
251
Hridya Valsaraju1bd37722018-01-12 10:25:59 -0800252Return<void> Health::getHealthInfo(getHealthInfo_cb _hidl_cb) {
253 using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
254
Yifan Honge9fc2352018-10-02 14:11:35 -0700255 updateAndNotify(nullptr);
Hridya Valsaraju1bd37722018-01-12 10:25:59 -0800256 struct android::BatteryProperties p = getBatteryProperties(battery_monitor_.get());
257
258 V1_0::HealthInfo batteryInfo;
259 convertToHealthInfo(&p, batteryInfo);
260
261 std::vector<StorageInfo> info;
262 get_storage_info(info);
263
264 std::vector<DiskStats> stats;
265 get_disk_stats(stats);
266
267 int32_t currentAvg = 0;
268
269 struct BatteryProperty prop;
270 status_t ret = battery_monitor_->getProperty(BATTERY_PROP_CURRENT_AVG, &prop);
271 if (ret == OK) {
272 currentAvg = static_cast<int32_t>(prop.valueInt64);
273 }
274
275 V2_0::HealthInfo healthInfo = {};
276 healthInfo.legacy = std::move(batteryInfo);
277 healthInfo.batteryCurrentAverage = currentAvg;
278 healthInfo.diskStats = stats;
279 healthInfo.storageInfos = info;
280
281 _hidl_cb(Result::SUCCESS, healthInfo);
282 return Void();
283}
284
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800285void Health::serviceDied(uint64_t /* cookie */, const wp<IBase>& who) {
286 (void)unregisterCallbackInternal(who.promote());
287}
288
289sp<IHealth> Health::initInstance(struct healthd_config* c) {
290 if (instance_ == nullptr) {
291 instance_ = new Health(c);
292 }
293 return instance_;
294}
295
296sp<Health> Health::getImplementation() {
297 CHECK(instance_ != nullptr);
298 return instance_;
299}
300
301} // namespace implementation
302} // namespace V2_0
303} // namespace health
304} // namespace hardware
305} // namespace android