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