blob: 96f6d70aedd9b5a1b358c385c8e537c166a3f263 [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
19#include <health2/Health.h>
20
21#include <hidl/HidlTransportSupport.h>
22
23extern void healthd_battery_update_internal(bool);
24
25namespace android {
26namespace hardware {
27namespace health {
28namespace V2_0 {
29namespace implementation {
30
31sp<Health> Health::instance_;
32
33Health::Health(struct healthd_config* c) {
34 // TODO(b/69268160): remove when libhealthd is removed.
35 healthd_board_init(c);
36 battery_monitor_ = std::make_unique<BatteryMonitor>();
37 battery_monitor_->init(c);
38}
39
40// Methods from IHealth follow.
41Return<Result> Health::registerCallback(const sp<IHealthInfoCallback>& callback) {
42 if (callback == nullptr) {
43 return Result::SUCCESS;
44 }
45
46 {
47 std::lock_guard<std::mutex> _lock(callbacks_lock_);
48 callbacks_.push_back(callback);
49 // unlock
50 }
51
52 auto linkRet = callback->linkToDeath(this, 0u /* cookie */);
53 if (!linkRet.withDefault(false)) {
54 LOG(WARNING) << __func__ << "Cannot link to death: "
55 << (linkRet.isOk() ? "linkToDeath returns false" : linkRet.description());
56 // ignore the error
57 }
58
59 return update();
60}
61
62bool Health::unregisterCallbackInternal(const sp<IBase>& callback) {
63 if (callback == nullptr) return false;
64
65 bool removed = false;
66 std::lock_guard<std::mutex> _lock(callbacks_lock_);
67 for (auto it = callbacks_.begin(); it != callbacks_.end();) {
68 if (interfacesEqual(*it, callback)) {
69 it = callbacks_.erase(it);
70 removed = true;
71 } else {
72 ++it;
73 }
74 }
75 (void)callback->unlinkToDeath(this).isOk(); // ignore errors
76 return removed;
77}
78
79Return<Result> Health::unregisterCallback(const sp<IHealthInfoCallback>& callback) {
80 return unregisterCallbackInternal(callback) ? Result::SUCCESS : Result::NOT_FOUND;
81}
82
83template <typename T>
84void getProperty(const std::unique_ptr<BatteryMonitor>& monitor, int id, T defaultValue,
85 const std::function<void(Result, T)>& callback) {
86 struct BatteryProperty prop;
87 T ret = defaultValue;
88 Result result = Result::SUCCESS;
89 status_t err = monitor->getProperty(static_cast<int>(id), &prop);
90 if (err != OK) {
91 LOG(DEBUG) << "getProperty(" << id << ")"
92 << " fails: (" << err << ") " << strerror(-err);
93 } else {
94 ret = static_cast<T>(prop.valueInt64);
95 }
96 switch (err) {
97 case OK:
98 result = Result::SUCCESS;
99 break;
100 case NAME_NOT_FOUND:
101 result = Result::NOT_SUPPORTED;
102 break;
103 default:
104 result = Result::UNKNOWN;
105 break;
106 }
107 callback(result, static_cast<T>(ret));
108}
109
110Return<void> Health::getChargeCounter(getChargeCounter_cb _hidl_cb) {
111 getProperty(battery_monitor_, BATTERY_PROP_CHARGE_COUNTER, INT32_MIN, _hidl_cb);
112 return Void();
113}
114
115Return<void> Health::getCurrentNow(getCurrentNow_cb _hidl_cb) {
116 getProperty(battery_monitor_, BATTERY_PROP_CURRENT_NOW, INT32_MIN, _hidl_cb);
117 return Void();
118}
119
120Return<void> Health::getCurrentAverage(getCurrentAverage_cb _hidl_cb) {
121 getProperty(battery_monitor_, BATTERY_PROP_CURRENT_AVG, INT32_MIN, _hidl_cb);
122 return Void();
123}
124
125Return<void> Health::getCapacity(getCapacity_cb _hidl_cb) {
126 getProperty(battery_monitor_, BATTERY_PROP_CAPACITY, INT32_MIN, _hidl_cb);
127 return Void();
128}
129
130Return<void> Health::getEnergyCounter(getEnergyCounter_cb _hidl_cb) {
131 getProperty(battery_monitor_, BATTERY_PROP_ENERGY_COUNTER, INT64_MIN, _hidl_cb);
132 return Void();
133}
134
135Return<void> Health::getChargeStatus(getChargeStatus_cb _hidl_cb) {
136 getProperty(battery_monitor_, BATTERY_PROP_BATTERY_STATUS, BatteryStatus::UNKNOWN, _hidl_cb);
137 return Void();
138}
139
140Return<Result> Health::update() {
141 if (!healthd_mode_ops || !healthd_mode_ops->battery_update) {
142 LOG(WARNING) << "health@2.0: update: not initialized. "
143 << "update() should not be called in charger / recovery.";
144 return Result::UNKNOWN;
145 }
146
147 // Retrieve all information and call healthd_mode_ops->battery_update, which calls
148 // notifyListeners.
149 bool chargerOnline = battery_monitor_->update();
150
151 // adjust uevent / wakealarm periods
152 healthd_battery_update_internal(chargerOnline);
153
154 return Result::SUCCESS;
155}
156
157void Health::notifyListeners(const HealthInfo& info) {
158 std::lock_guard<std::mutex> _lock(callbacks_lock_);
159 for (auto it = callbacks_.begin(); it != callbacks_.end();) {
160 auto ret = (*it)->healthInfoChanged(info);
161 if (!ret.isOk() && ret.isDeadObject()) {
162 it = callbacks_.erase(it);
163 } else {
164 ++it;
165 }
166 }
167}
168
169Return<void> Health::debug(const hidl_handle& handle, const hidl_vec<hidl_string>&) {
170 if (handle != nullptr && handle->numFds >= 1) {
171 int fd = handle->data[0];
172 battery_monitor_->dumpState(fd);
173 fsync(fd);
174 }
175 return Void();
176}
177
Hridya Valsaraju2b520832017-12-20 13:25:29 -0800178Return<void> Health::getStorageInfo(getStorageInfo_cb _hidl_cb) {
179 std::vector<struct StorageInfo> info;
180 get_storage_info(info);
181 hidl_vec<struct StorageInfo> info_vec(info);
182 if (!info.size()) {
183 _hidl_cb(Result::NOT_SUPPORTED, info_vec);
184 } else {
185 _hidl_cb(Result::SUCCESS, info_vec);
186 }
187 return Void();
188}
189
190Return<void> Health::getDiskStats(getDiskStats_cb _hidl_cb) {
191 std::vector<struct DiskStats> stats;
192 get_disk_stats(stats);
193 hidl_vec<struct DiskStats> stats_vec(stats);
194 if (!stats.size()) {
195 _hidl_cb(Result::NOT_SUPPORTED, stats_vec);
196 } else {
197 _hidl_cb(Result::SUCCESS, stats_vec);
198 }
199 return Void();
200}
201
Hridya Valsarajud3e3d722017-12-11 17:31:00 -0800202void Health::serviceDied(uint64_t /* cookie */, const wp<IBase>& who) {
203 (void)unregisterCallbackInternal(who.promote());
204}
205
206sp<IHealth> Health::initInstance(struct healthd_config* c) {
207 if (instance_ == nullptr) {
208 instance_ = new Health(c);
209 }
210 return instance_;
211}
212
213sp<Health> Health::getImplementation() {
214 CHECK(instance_ != nullptr);
215 return instance_;
216}
217
218} // namespace implementation
219} // namespace V2_0
220} // namespace health
221} // namespace hardware
222} // namespace android