blob: 65eada85d0e824dfb68e925852456d1445564393 [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>
Yifan Hong00ebc752019-10-07 12:25:23 -070020#include <android/hardware/health/2.0/types.h>
Hridya Valsarajud3e3d722017-12-11 17:31:00 -080021#include <health2/Health.h>
22
Hridya Valsaraju1bd37722018-01-12 10:25:59 -080023#include <hal_conversion.h>
Hridya Valsarajud3e3d722017-12-11 17:31:00 -080024#include <hidl/HidlTransportSupport.h>
25
Yifan Hong00ebc752019-10-07 12:25:23 -070026using HealthInfo_1_0 = android::hardware::health::V1_0::HealthInfo;
27using android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo;
28
Hridya Valsarajud3e3d722017-12-11 17:31:00 -080029extern void healthd_battery_update_internal(bool);
30
31namespace android {
32namespace hardware {
33namespace health {
34namespace V2_0 {
35namespace implementation {
36
37sp<Health> Health::instance_;
38
39Health::Health(struct healthd_config* c) {
40 // TODO(b/69268160): remove when libhealthd is removed.
41 healthd_board_init(c);
42 battery_monitor_ = std::make_unique<BatteryMonitor>();
43 battery_monitor_->init(c);
44}
45
46// Methods from IHealth follow.
47Return<Result> Health::registerCallback(const sp<IHealthInfoCallback>& callback) {
48 if (callback == nullptr) {
49 return Result::SUCCESS;
50 }
51
52 {
Yifan Honga46c0da2018-10-02 14:06:51 -070053 std::lock_guard<decltype(callbacks_lock_)> lock(callbacks_lock_);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -080054 callbacks_.push_back(callback);
55 // unlock
56 }
57
58 auto linkRet = callback->linkToDeath(this, 0u /* cookie */);
59 if (!linkRet.withDefault(false)) {
60 LOG(WARNING) << __func__ << "Cannot link to death: "
61 << (linkRet.isOk() ? "linkToDeath returns false" : linkRet.description());
62 // ignore the error
63 }
64
Yifan Honge9fc2352018-10-02 14:11:35 -070065 return updateAndNotify(callback);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -080066}
67
68bool Health::unregisterCallbackInternal(const sp<IBase>& callback) {
69 if (callback == nullptr) return false;
70
71 bool removed = false;
Yifan Honga46c0da2018-10-02 14:06:51 -070072 std::lock_guard<decltype(callbacks_lock_)> lock(callbacks_lock_);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -080073 for (auto it = callbacks_.begin(); it != callbacks_.end();) {
74 if (interfacesEqual(*it, callback)) {
75 it = callbacks_.erase(it);
76 removed = true;
77 } else {
78 ++it;
79 }
80 }
81 (void)callback->unlinkToDeath(this).isOk(); // ignore errors
82 return removed;
83}
84
85Return<Result> Health::unregisterCallback(const sp<IHealthInfoCallback>& callback) {
86 return unregisterCallbackInternal(callback) ? Result::SUCCESS : Result::NOT_FOUND;
87}
88
89template <typename T>
90void getProperty(const std::unique_ptr<BatteryMonitor>& monitor, int id, T defaultValue,
91 const std::function<void(Result, T)>& callback) {
92 struct BatteryProperty prop;
93 T ret = defaultValue;
94 Result result = Result::SUCCESS;
95 status_t err = monitor->getProperty(static_cast<int>(id), &prop);
96 if (err != OK) {
97 LOG(DEBUG) << "getProperty(" << id << ")"
98 << " fails: (" << err << ") " << strerror(-err);
99 } else {
100 ret = static_cast<T>(prop.valueInt64);
101 }
102 switch (err) {
103 case OK:
104 result = Result::SUCCESS;
105 break;
106 case NAME_NOT_FOUND:
107 result = Result::NOT_SUPPORTED;
108 break;
109 default:
110 result = Result::UNKNOWN;
111 break;
112 }
113 callback(result, static_cast<T>(ret));
114}
115
116Return<void> Health::getChargeCounter(getChargeCounter_cb _hidl_cb) {
Yifan Hong81874af2018-01-17 10:59:12 -0800117 getProperty<int32_t>(battery_monitor_, BATTERY_PROP_CHARGE_COUNTER, 0, _hidl_cb);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800118 return Void();
119}
120
121Return<void> Health::getCurrentNow(getCurrentNow_cb _hidl_cb) {
Yifan Hong81874af2018-01-17 10:59:12 -0800122 getProperty<int32_t>(battery_monitor_, BATTERY_PROP_CURRENT_NOW, 0, _hidl_cb);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800123 return Void();
124}
125
126Return<void> Health::getCurrentAverage(getCurrentAverage_cb _hidl_cb) {
Yifan Hong81874af2018-01-17 10:59:12 -0800127 getProperty<int32_t>(battery_monitor_, BATTERY_PROP_CURRENT_AVG, 0, _hidl_cb);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800128 return Void();
129}
130
131Return<void> Health::getCapacity(getCapacity_cb _hidl_cb) {
Yifan Hong81874af2018-01-17 10:59:12 -0800132 getProperty<int32_t>(battery_monitor_, BATTERY_PROP_CAPACITY, 0, _hidl_cb);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800133 return Void();
134}
135
136Return<void> Health::getEnergyCounter(getEnergyCounter_cb _hidl_cb) {
Yifan Hong81874af2018-01-17 10:59:12 -0800137 getProperty<int64_t>(battery_monitor_, BATTERY_PROP_ENERGY_COUNTER, 0, _hidl_cb);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800138 return Void();
139}
140
141Return<void> Health::getChargeStatus(getChargeStatus_cb _hidl_cb) {
142 getProperty(battery_monitor_, BATTERY_PROP_BATTERY_STATUS, BatteryStatus::UNKNOWN, _hidl_cb);
143 return Void();
144}
145
146Return<Result> Health::update() {
147 if (!healthd_mode_ops || !healthd_mode_ops->battery_update) {
148 LOG(WARNING) << "health@2.0: update: not initialized. "
Hridya Valsarajubf0b9fa2018-09-28 10:44:02 -0700149 << "update() should not be called in charger";
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800150 return Result::UNKNOWN;
151 }
152
153 // Retrieve all information and call healthd_mode_ops->battery_update, which calls
154 // notifyListeners.
Yifan Hong9521b9d2019-10-07 10:47:02 -0700155 battery_monitor_->updateValues();
Yifan Hong00ebc752019-10-07 12:25:23 -0700156 const HealthInfo_1_0& health_info = battery_monitor_->getHealthInfo_1_0();
157 struct BatteryProperties props;
158 convertFromHealthInfo(health_info, &props);
Stephane Lee45f8ab32020-02-14 11:56:43 -0800159 bool log = (healthd_board_battery_update(&props) == 0);
Yifan Hong9521b9d2019-10-07 10:47:02 -0700160 if (log) {
161 battery_monitor_->logValues();
162 }
163 healthd_mode_ops->battery_update(&props);
164 bool chargerOnline = battery_monitor_->isChargerOnline();
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800165
166 // adjust uevent / wakealarm periods
167 healthd_battery_update_internal(chargerOnline);
168
169 return Result::SUCCESS;
170}
171
Yifan Honge9fc2352018-10-02 14:11:35 -0700172Return<Result> Health::updateAndNotify(const sp<IHealthInfoCallback>& callback) {
173 std::lock_guard<decltype(callbacks_lock_)> lock(callbacks_lock_);
174 std::vector<sp<IHealthInfoCallback>> storedCallbacks{std::move(callbacks_)};
175 callbacks_.clear();
176 if (callback != nullptr) {
177 callbacks_.push_back(callback);
178 }
179 Return<Result> result = update();
180 callbacks_ = std::move(storedCallbacks);
181 return result;
182}
183
Hridya Valsarajud31932a2018-01-17 23:09:24 -0800184void Health::notifyListeners(HealthInfo* healthInfo) {
185 std::vector<StorageInfo> info;
186 get_storage_info(info);
187
188 std::vector<DiskStats> stats;
189 get_disk_stats(stats);
190
191 int32_t currentAvg = 0;
192
193 struct BatteryProperty prop;
194 status_t ret = battery_monitor_->getProperty(BATTERY_PROP_CURRENT_AVG, &prop);
195 if (ret == OK) {
196 currentAvg = static_cast<int32_t>(prop.valueInt64);
197 }
198
199 healthInfo->batteryCurrentAverage = currentAvg;
200 healthInfo->diskStats = stats;
201 healthInfo->storageInfos = info;
202
Yifan Honga46c0da2018-10-02 14:06:51 -0700203 std::lock_guard<decltype(callbacks_lock_)> lock(callbacks_lock_);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800204 for (auto it = callbacks_.begin(); it != callbacks_.end();) {
Hridya Valsarajud31932a2018-01-17 23:09:24 -0800205 auto ret = (*it)->healthInfoChanged(*healthInfo);
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800206 if (!ret.isOk() && ret.isDeadObject()) {
207 it = callbacks_.erase(it);
208 } else {
209 ++it;
210 }
211 }
212}
213
214Return<void> Health::debug(const hidl_handle& handle, const hidl_vec<hidl_string>&) {
215 if (handle != nullptr && handle->numFds >= 1) {
216 int fd = handle->data[0];
217 battery_monitor_->dumpState(fd);
Yifan Hong81b28332018-04-04 15:28:19 -0700218
219 getHealthInfo([fd](auto res, const auto& info) {
220 android::base::WriteStringToFd("\ngetHealthInfo -> ", fd);
221 if (res == Result::SUCCESS) {
222 android::base::WriteStringToFd(toString(info), fd);
223 } else {
224 android::base::WriteStringToFd(toString(res), fd);
225 }
226 android::base::WriteStringToFd("\n", fd);
227 });
228
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800229 fsync(fd);
230 }
231 return Void();
232}
233
Hridya Valsaraju2b520832017-12-20 13:25:29 -0800234Return<void> Health::getStorageInfo(getStorageInfo_cb _hidl_cb) {
235 std::vector<struct StorageInfo> info;
236 get_storage_info(info);
237 hidl_vec<struct StorageInfo> info_vec(info);
238 if (!info.size()) {
239 _hidl_cb(Result::NOT_SUPPORTED, info_vec);
240 } else {
241 _hidl_cb(Result::SUCCESS, info_vec);
242 }
243 return Void();
244}
245
246Return<void> Health::getDiskStats(getDiskStats_cb _hidl_cb) {
247 std::vector<struct DiskStats> stats;
248 get_disk_stats(stats);
249 hidl_vec<struct DiskStats> stats_vec(stats);
250 if (!stats.size()) {
251 _hidl_cb(Result::NOT_SUPPORTED, stats_vec);
252 } else {
253 _hidl_cb(Result::SUCCESS, stats_vec);
254 }
255 return Void();
256}
257
Hridya Valsaraju1bd37722018-01-12 10:25:59 -0800258Return<void> Health::getHealthInfo(getHealthInfo_cb _hidl_cb) {
259 using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
260
Yifan Honge9fc2352018-10-02 14:11:35 -0700261 updateAndNotify(nullptr);
Yifan Hong00ebc752019-10-07 12:25:23 -0700262 HealthInfo healthInfo = battery_monitor_->getHealthInfo_2_0();
Hridya Valsaraju1bd37722018-01-12 10:25:59 -0800263
264 std::vector<StorageInfo> info;
265 get_storage_info(info);
266
267 std::vector<DiskStats> stats;
268 get_disk_stats(stats);
269
270 int32_t currentAvg = 0;
271
272 struct BatteryProperty prop;
273 status_t ret = battery_monitor_->getProperty(BATTERY_PROP_CURRENT_AVG, &prop);
274 if (ret == OK) {
275 currentAvg = static_cast<int32_t>(prop.valueInt64);
276 }
277
Hridya Valsaraju1bd37722018-01-12 10:25:59 -0800278 healthInfo.batteryCurrentAverage = currentAvg;
279 healthInfo.diskStats = stats;
280 healthInfo.storageInfos = info;
281
282 _hidl_cb(Result::SUCCESS, healthInfo);
283 return Void();
284}
285
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800286void Health::serviceDied(uint64_t /* cookie */, const wp<IBase>& who) {
287 (void)unregisterCallbackInternal(who.promote());
288}
289
290sp<IHealth> Health::initInstance(struct healthd_config* c) {
291 if (instance_ == nullptr) {
292 instance_ = new Health(c);
293 }
294 return instance_;
295}
296
297sp<Health> Health::getImplementation() {
298 CHECK(instance_ != nullptr);
299 return instance_;
300}
301
302} // namespace implementation
303} // namespace V2_0
304} // namespace health
305} // namespace hardware
306} // namespace android